C语言 如何在C中创建一个新的文本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34008206/
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
How to create a new text file in C?
提问by Hassen Fatima
I am creating a program which reads data from one text file and changes it size to upper or lower case and then stores that data in a new file. I have searched the internet, but I can't find how to create a new text file.
我正在创建一个程序,它从一个文本文件中读取数据并将其大小更改为大写或小写,然后将该数据存储在一个新文件中。我在互联网上搜索过,但找不到如何创建新的文本文件。
#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");
I know this is incomplete, but I can't figure out how to crate a new text file!
我知道这是不完整的,但我不知道如何创建一个新的文本文件!
回答by Ishamael
fp = fopen("textFile.txt" ,"a");
This is a correct way to create a text file. The issue is with your printfstatements. What you want instead is:
这是创建文本文件的正确方法。问题出在你的printf陈述上。你想要的是:
fprintf(fp, "Change Case \n");
...
回答by Dostonbek Oripjonov
#include <stdio.h>
#define FILE_NAME "text.txt"
int main()
{
FILE* file_ptr = fopen(FILE_NAME, "w");
fclose(file_ptr);
return 0;
}

