如何在C中创建新的文本文件?

Has*_*ima 1 c text-files

我正在创建一个程序,它从一个文本文件中读取数据并将其大小更改为大写或小写,然后将该数据存储在新文件中.我搜索过互联网,但我找不到如何创建新的文本文件.

#include <stdio.h>
int main(void) {

        FILE *fp = NULL;

        fp = fopen("textFile.txt" ,"a");

        char choice;

        if (fp != NULL) {

                printf("Change Case \n");
                printf("============\n");
                printf("Case (U for upper, L for lower) : ");
                scanf(" %c", &choice);
                printf("Name of the original file : textFile.txt \n");
                printf("Name of the updated file : newFile.txt \n");
Run Code Online (Sandbox Code Playgroud)

我知道这是不完整的,但我无法弄清楚如何创建一个新的文本文件!

Ish*_*ael 6

fp = fopen("textFile.txt" ,"a");
Run Code Online (Sandbox Code Playgroud)

这是创建文本文件的正确方法.问题出在你的printf陈述上.你想要的是:

fprintf(fp, "Change Case \n");
...
Run Code Online (Sandbox Code Playgroud)

  • @HassenFatima,它将“更改案例”写入处理 `fp` 指向的文件。与常规的 `printf` 不同,它写入标准输出。 (2认同)