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

问题描述 投票:0回答:7

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

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

"r+"
打开文本文件进行更新(即用于阅读和更新) 写作)。
"w+"
打开文本文件进行更新(读取和写入), 第一次截断 如果文件存在,则将其长度为零;如果不存在,则创建文件。

我的意思是,区别在于,如果我用

"w+"
打开文件,文件将首先被删除?

c fopen
7个回答
96
投票

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


82
投票

这是显示如何选择文件模式的快速参考图:

Flow Diagram for fopen controls

请注意,

a+
w
w+
模式将创建一个新文件(如果该文件尚不存在)。


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+
并查看结果。
以下是不同文件模式的总结:


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

测试.txt

This is testing for fprintf...

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

输出

This is testing for fprintf...

测试.txt

This is testing for fprintf...

r+

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

测试.txt

This is testing for fprintf again...

rw 形成 r+

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

测试.txt

This is testing for fprintf again...

a+

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

测试.txt

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

ar 形成 a+

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

测试.txt

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

3
投票

有 2 个区别,与

r+
不同,
w+
会:

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

0
投票

r+ 将现有文件从头打开以进行读取和写入。 w+ 与 w 相同,但读取和写入均不同。

© www.soinside.com 2019 - 2024. All rights reserved.