在fopen("myfile", "r+")什么之间的区别"r+"和"w+"开放模式?我看了这个:
"r"打开文本文件进行阅读."w"打开文本文件以进行写入,将现有文件截断为零长度,或者创建文件(如果该文件不存在).
"r+"打开文本文件进行更新(即读取和写入)."w+"打开文本文件以进行更新(读取和写入),首先将文件截断为零长度(如果存在)或创建文件(如果文件不存在).
我的意思是不同的是,如果我打开文件"w+",文件将被删除?
Pet*_*ter 72
双方r+并w+可以读取和写入文件.但是,r+如果此类文件不存在,则不会删除文件的内容并且不会创建新文件,而w+删除文件的内容并在文件不存在时创建它.
Wak*_*eem 43
下次阅读此图会更快。也许其他人也会觉得这很有帮助。这清楚地解释了两者之间的区别。 
hac*_*cks 29
试试这些代码,您就会明白:
#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);
}
Run Code Online (Sandbox Code Playgroud)
然后这个
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "w+");
fclose(fp);
}
Run Code Online (Sandbox Code Playgroud)
然后打开文件w+,看看会发生什么.您将看到第一个程序写入的所有数据都已被删除.
重复此操作r+并查看结果.希望你能理解.
inv*_*_id 18
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)
Run Code Online (Sandbox Code Playgroud)
所以是的,如果文件已经存在,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);
}
Run Code Online (Sandbox Code Playgroud)
输出
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
测试.txt
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
输出
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
测试.txt
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
r+
测试.txt
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
测试.txt
This is testing for fprintf again...
Run Code Online (Sandbox Code Playgroud)
r和w形成r+
测试.txt
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
测试.txt
This is testing for fprintf again...
Run Code Online (Sandbox Code Playgroud)
一个+
测试.txt
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
测试.txt
This is testing for fprintf...
This is testing for fprintf again...
Run Code Online (Sandbox Code Playgroud)
a和r形成a+
测试.txt
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出
This is testing for fprintf...
Run Code Online (Sandbox Code Playgroud)
测试.txt
This is testing for fprintf...
This is testing for fprintf again...
Run Code Online (Sandbox Code Playgroud)