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

时间:2014-01-14 12:46:41

标签: c fopen

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

  

"r"打开文本文件进行阅读。
"w"打开文字文件进行书写,   将现有文件截断为零长度,或者如果文件不存在则创建该文件。

     

"r+"打开一个文本文件进行更新(即阅读和阅读   写作)。
"w+"打开文本文件进行更新(读写),   先截断       文件如果存在则为零长度,如果文件不存在则创建文件。

我的意思是区别在于,如果我用"w+"打开文件,文件将首先被删除?

7 个答案:

答案 0 :(得分:69)

r+w+都可以读取和写入文件。但是,r+不会删除文件的内容,并且如果此文件不存在则不会创建新文件,而w+会删除文件的内容并创建它(如果它不存在)不存在。

答案 1 :(得分:24)

主要区别在于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+重复此操作并查看结果。希望你能理解。

答案 2 :(得分:17)

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

答案 3 :(得分:4)

此图在下一次阅读时会更快。也许其他人也会发现它也有帮助。这清楚地解释了两者之间的区别。 enter image description here

答案 4 :(得分:3)

r+不同,有两点不同,w+会:

  • 创建文件(如果该文件尚不存在)
  • 首先截断它,即删除其内容

答案 5 :(得分:1)

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

r w 组成 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...

a r 形成 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...

答案 6 :(得分:0)

r +现有文件在读取和写入时都打开。 除了阅读和写作外,w +与w相同。