fopen()中r +和w +之间的差异

yay*_*zis 48 c fopen

fopen("myfile", "r+")什么之间的区别"r+""w+"开放模式?我看了这个:

"r" 打开文本文件进行阅读.
"w"打开文本文件以进行写入,将现有文件截断为零长度,或者创建文件(如果该文件不存在).

"r+"打开文本文件进行更新(即读取和写入).
"w+"打开文本文件以进行更新(读取和写入),首先将文件截断为零长度(如果存在)或创建文件(如果文件不存在).

我的意思是不同的是,如果我打开文件"w+",文件将被删除?

Pet*_*ter 72

双方r+w+可以读取和写入文件.但是,r+如果此类文件不存在,则不会删除文件的内容并且不会创建新文件,而w+删除文件的内容并在文件不存在时创建它.

  • 我正在寻找一种不删除文件内容的方法(例如r +),但也可以创建一个不存在的新文件(例如w +)。我发现`open(x,'a')。close(); 打开(x,'r +')` (2认同)

Wak*_*eem 43

下次阅读此图会更快。也许其他人也会觉得这很有帮助。这清楚地解释了两者之间的区别。 在此处输入图片说明

  • @Irtza Shahan 不完全一样,你说的都是正确的。但是,除此之外,您还可以使用“a+”来读取文件的内容。您可能认为这没有意义,因为初始位置是 End,但请记住您可以在之后移动指针。 (2认同)

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+并查看结果.希望你能理解.

  • 这并没有回答这个问题。 (6认同)

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 +将删除文件并给你一个空文件.


Sat*_*vik 6

瓦+

#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)

wr形成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)

rw形成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)

ar形成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)

  • 以我的愚见为最佳答案。应该是公认的。 (3认同)