IT이야기

fopen()의 r+와 w+의 차이

cyworld 2022. 6. 8. 23:41
반응형

fopen()의 r+와 w+의 차이

fopen("myfile", "r+")와의 차이는 무엇입니까?"r+"그리고."w+"오픈 모드?읽었어요.

"r"읽을 텍스트 파일을 엽니다.
"w"쓰기, 기존 파일 길이 0으로 잘라내기 또는 파일이 없는 경우 파일을 만들기 위해 텍스트 파일을 엽니다.

"r+"업데이트(읽기와 쓰기 모두)하기 위해 텍스트 파일을 엽니다.
"w+"업데이트(읽기 및 쓰기)를 위해 텍스트 파일을 엽니다. 먼저 파일이 있으면 0 길이로 잘라내고 없으면 파일을 만듭니다.

제 말은, 다른 점은 만약 제가 이 파일을"w+", 파일이 먼저 지워집니다.

이 그림은 다음 번에 읽는 것이 더 빠를 것입니다.아마 다른 사람도 도움이 될 거야이것은 그 차이를 명확하게 설명한다.여기에 이미지 설명 입력

가장 큰 차이점은w+파일이 있는 경우 0 길이로 잘라내고 없는 경우 새 파일을 만듭니다.하는 동안에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+결과를 봐야지.
다음은 다양한 파일 모드의 요약입니다(1 = >ture, 0 =>false):

여기에 이미지 설명 입력

둘다요.r+그리고.w+파일을 읽고 쓸 수 있습니다.하지만,r+는, 파일의 내용을 삭제하지 않고, 그러한 파일이 존재하지 않는 경우는 새로운 파일을 작성하지 않습니다.w+파일 내용을 삭제하고 존재하지 않는 경우 작성합니다.

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+는 파일을 지우고 빈 파일을 제공합니다.

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+를 형성하는 a와 r

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

두 가지 차이점이 있습니다.r+,w+의 내용:

  • 파일이 아직 존재하지 않는 경우 생성
  • 먼저 잘라냅니다. 즉, 내용을 삭제합니다.

r+ 기존 파일은 읽기 및 쓰기를 위해 처음부터 열립니다. w+ 읽기 및 쓰기를 제외하고 w와 동일합니다.

언급URL : https://stackoverflow.com/questions/21113919/difference-between-r-and-w-in-fopen

반응형