在
fopen("myfile", "r+")
中,"r+"
和"w+"
打开模式有什么区别?我读到了这个:
打开文本文件进行阅读。"r"
打开一个文本文件进行写入, 将现有文件截断为零长度,或者创建该文件(如果不存在)。"w"
打开文本文件进行更新(即用于阅读和更新) 写作)。"r+"
打开文本文件进行更新(读取和写入), 第一次截断 如果文件存在,则将其长度为零;如果不存在,则创建文件。"w+"
我的意思是,区别在于,如果我用
"w+"
打开文件,文件将首先被删除?
r+
和w+
都可以读取和写入文件。但是,r+
不会删除文件的内容,如果文件不存在,也不会创建新文件,而 w+
会删除文件的内容,如果不存在则创建新文件。
这是显示如何选择文件模式的快速参考图:
请注意,
a+
、w
和w+
模式将创建一个新文件(如果该文件尚不存在)。
主要区别是
w+
如果文件存在则将其截断为零长度,如果不存在则创建一个新文件。而 r+
既不会删除内容,也不会在不存在的情况下创建新文件。
尝试这些代码,你就会明白:
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
然后这个
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "w+");
fclose(fp);
}
如果你打开
test.txt
,你会看到第一个程序写入的所有数据都已被删除。r+
并查看结果。r = read mode only
r+ = read/write mode
w = write mode only
w+ = read/write mode, if the file already exists override it (empty it)
所以是的,如果文件已经存在,w+将删除该文件并给你一个空文件。
w+
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "w+"); //write and read mode
fprintf(fp, "This is testing for fprintf...\n");
rewind(fp); //rewind () function moves file pointer position to the beginning of the file.
char ch;
while((ch=getc(fp))!=EOF)
putchar(ch);
fclose(fp);
}
输出
This is testing for fprintf...
测试.txt
This is testing for fprintf...
w 和 r 形成 w+
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "w"); //only write mode
fprintf(fp, "This is testing for fprintf...\n");
fclose(fp);
fp = fopen("test.txt", "r");
char ch;
while((ch=getc(fp))!=EOF)
putchar(ch);
fclose(fp);
}
输出
This is testing for fprintf...
测试.txt
This is testing for fprintf...
r+
测试.txt
This is testing for fprintf...
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "r+"); //read and write mode
char ch;
while((ch=getc(fp))!=EOF)
putchar(ch);
rewind(fp); //rewind () function moves file pointer position to the beginning of the file.
fprintf(fp, "This is testing for fprintf again...\n");
fclose(fp);
return 0;
}
输出
This is testing for fprintf...
测试.txt
This is testing for fprintf again...
r 和 w 形成 r+
测试.txt
This is testing for fprintf...
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "r");
char ch;
while((ch=getc(fp))!=EOF)
putchar(ch);
fclose(fp);
fp=fopen("test.txt","w");
fprintf(fp, "This is testing for fprintf again...\n");
fclose(fp);
return 0;
}
输出
This is testing for fprintf...
测试.txt
This is testing for fprintf again...
a+
测试.txt
This is testing for fprintf...
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "a+"); //append and read mode
char ch;
while((ch=getc(fp))!=EOF)
putchar(ch);
rewind(fp); //rewind () function moves file pointer position to the beginning of the file.
fprintf(fp, "This is testing for fprintf again...\n");
fclose(fp);
return 0;
}
输出
This is testing for fprintf...
测试.txt
This is testing for fprintf...
This is testing for fprintf again...
a 和 r 形成 a+
测试.txt
This is testing for fprintf...
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "a"); //append and read mode
char ch;
while((ch=getc(fp))!=EOF)
putchar(ch);
fclose(fp);
fp=fopen("test.txt","r");
fprintf(fp, "This is testing for fprintf again...\n");
fclose(fp);
return 0;
}
输出
This is testing for fprintf...
测试.txt
This is testing for fprintf...
This is testing for fprintf again...
有 2 个区别,与
r+
不同,w+
会:
r+ 将现有文件从头打开以进行读取和写入。 w+ 与 w 相同,但读取和写入均不同。