C语言 fopen() 中 r+ 和 w+ 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21113919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:37:14  来源:igfitidea点击:

Difference between r+ and w+ in fopen()

cfopen

提问by yaylitzis

In fopen("myfile", "r+")what is the difference between the "r+"and "w+"open mode? I read this:

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

"r"Open a text file for reading.
"w"Open a text file for writing, truncating an an existing file to zero length, or creating the file if it does not exist.

"r+"Open a text file for update (that is, for both reading and writing).
"w+"Open a text file for update (reading and writing), first truncating the file to zero length if it exists or creating the file if it does not exist.

"r"打开一个文本文件进行阅读。
"w"打开文本文件以进行写入、将现有文件截断为零长度或在文件不存在时创建该文件。

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

I mean the difference is that if I open the file with "w+", the file will be erased first?

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

采纳答案by haccks

The main difference is w+truncate the file to zero length if it exists or create a new file if it doesn't. While r+neither deletes the content nor create a new file if it doesn't exist.

主要区别是w+将文件截断为零长度(如果存在)或创建新文件(如果不存在)。虽然r+没有删除该内容,也没有创建一个新的文件,如果它不存在。

Try these codes and you will understand:

试试这些代码,你就会明白:

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

and then this

然后这个

#include <stdio.h>
int main()
{
   FILE *fp;

   fp = fopen("test.txt", "w+");
   fclose(fp);
}   

Then open the file test.txtand see the what happens. You will see that all data written by the first program has been erased.
Repeat this for r+and see the result. Hope you will understand.

然后打开文件test.txt,看看会发生什么。您将看到第一个程序写入的所有数据都已被擦除。
重复此操作r+并查看结果。希望你会明白。

回答by Peter

Both r+and w+can read and write to a file. However, r+doesn't delete the content of the file and doesn't create a new file if such file doesn't exist, whereas w+deletes the content of the file and creates it if it doesn't exist.

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

回答by invalid_id

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)

So yes, if the file already exists w+ will erase the file and give you an empty file.

所以是的,如果文件已经存在 w+ 将删除文件并给你一个空文件。

回答by Waqar Naeem

This diagram will be faster to read next time. Maybe someone else will find that helpful too. This is clearly explained the difference between. enter image description here

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

回答by Sathvik

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);
}  

output

输出

This is testing for fprintf...

test.txt

测试.txt

This is testing for fprintf...


wand rto form w+

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);
}  

output

输出

This is testing for fprintf...

test.txt

测试.txt

This is testing for fprintf...


r+

r+

test.txt

测试.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;
}

output

输出

This is testing for fprintf...

test.txt

测试.txt

This is testing for fprintf again...


rand wto form r+

rw形成r+

test.txt

测试.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;
}

output

输出

This is testing for fprintf...

test.txt

测试.txt

This is testing for fprintf again...


a+

一个+

test.txt

测试.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;
}

output

输出

This is testing for fprintf...

test.txt

测试.txt

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


aand rto form a+

ar形成a+

test.txt

测试.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;
}

output

输出

This is testing for fprintf...

test.txt

测试.txt

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

回答by perreal

There are 2 differences, unlike r+, w+will:

有2个区别,不像r+w+将:

  • create the file if it does not already exist
  • first truncate it, i.e., will delete its contents
  • 如果文件不存在,则创建该文件
  • 首先截断它,即会删除它的内容

回答by Ashwani Bansal

r+ The existing file is opened to the beginning for both reading and writing. w+ Same as w except both for reading and writing.

r+ 现有的文件被打开到开头进行读取和写入。w+ 与 w 相同,除了读和写。