写这个文件句柄有什么问题?

1 c file-io segmentation-fault

#include<stdio.h>
#include<ctype.h>

int main() {

    FILE *fpin = fopen("in.txt", "r");
    fprintf(fpin, "hello, world!");
    fclose (fpin);

    char c;
    fpin = fopen("in.txt", "r");
    FILE *fpout = fopen("out.txt", "w");
    while ((c = fgetc(fpin)) != EOF) {
        fputc (toupper(c), fpout);
    }

    fclose (fpout);
    fclose (fpin);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到了一个

分段故障

谢谢.

Gui*_*ume 10

我对C没有任何了解,但似乎你写了一个你已经打开的文件只读...

FILE *fpin = fopen("in.txt", "r"); 
fprintf(fpin, "hello, world!"); 
Run Code Online (Sandbox Code Playgroud)

应该是:

FILE *fpin = fopen("in.txt", "w"); 
fprintf(fpin, "hello, world!"); 
Run Code Online (Sandbox Code Playgroud)