fopen()中r+和w+的区别

90
fopen("myfile", "r+")中,"r+""w+"打开模式有什么区别?我看到了这个:

"r": 打开一个文本文件以供读取。
"w": 打开一个文本文件以供写入, 将现有文件截断为零长度,如果文件不存在,创建该文件。

"r+":打开一个文本文件进行更新(即同时进行读取和写入)。
"w+":打开一个文本文件进行更新(即读取和写入), 如果文件存在,则首先将文件截断为零长度;如果文件不存在,则创建该文件。

我的意思是,使用"w+"打开文件的区别在于文件会被首先清空吗?


1
两个区别就在这里:截断和创建。 - user2404501
2
如果使用w+模式打开文件,它将首先被“擦除”,或者在不存在时创建它(而r+模式在这种情况下会出错)。 - ShinTakezou
这个回答解决了你的问题吗?内置open函数中a、a+、w、w+和r+模式之间的区别是什么? - Yogev Neumann
7个回答

95

r+w+都可以读写文件,但是r+不会删除文件内容,如果文件不存在也不会创建新文件,而w+会删除文件内容并在文件不存在时创建新文件。


6
我在寻找一种方法,既不删除文件内容(像 r+ 模式),又可以在文件不存在时创建新文件(像 w+ 模式)。我发现可以使用 open(x, 'a').close(); open(x, 'r+') 实现。 - cowlinator

77

下次阅读这个图表会更快速。或许其他人也会发现这有用。这清晰地解释了它们的区别。enter image description here


2
流程图是最好的:D - Srinath Ganesh
这是黄金! - Gregor Hartl Watters
3
我喜欢这个图示,但是"创建新文件"的场景没有包含在其中,而这个场景在被接受的答案中有提到。 - MarcusS
刚刚发现,"a" 不允许寻找写入。 "a+" 的文档说明 "输出总是附加到文件的末尾",因此对于 "a" 也可能是正确的。 - Anonymous
所以a和a+之间没有区别,两者都用于写入,都不用于截断,初始位置都在末尾。 - Irtza Shahan
1
@Irtza Shahan 不完全相同,你说的都对。但除此之外,你还可以使用"a+"来读取文件内容。你可能会觉得因为初始位置在末尾,所以没有意义,但是记住你之后可以移动指针。 - Ganathor

63

主要区别在于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+ 重复这个过程并查看结果。
下面是不同文件模式的总结:

enter image description here


7
这并不回答问题。 - Cengiz Kandemir

25
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+ 将擦除该文件并给您一个空文件。


6

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...

test.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...

test.txt

This is testing for fprintf...

r+

test.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...

test.txt

This is testing for fprintf again...

rw 合并成 r+

test.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...

test.txt

This is testing for fprintf again...

a+

test.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...

test.txt

This is testing for fprintf...
This is testing for fprintf again...

使用 ar 来形成 a+

test.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...

test.txt

This is testing for fprintf...
This is testing for fprintf again...

3
我个人认为最佳答案。应该被接受。 - Sopalajo de Arrierez
如果在r+模式下打印的字符串比初始test.text文件中的字符串短,例如fprintf(fp, "Hello");会发生什么?它会变成简单的"Hello"还是"Hellois testing fprintf again..."? - Janjan
@Janjan 它打印出了“你好”。 - Sathvik

3

有2个不同之处,与 r + 不同,w + 将会:

  • 如果文件不存在,则创建该文件
  • 首先将其截断,即删除其内容

0

r+ 打开现有文件以进行读取和写入操作。 w+ 与 w 相同,但可同时进行读取和写入操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接