How can I export the output result in C language to a txt document?
In the C language, file operation functions can be used to export the output results as a txt document.
Here is an example code:
#include <stdio.h>
int main() {
FILE *fp; // 文件指针
char data[] = "Hello, world!"; // 要输出的内容
// 打开文件,如果文件不存在则创建新文件
fp = fopen("output.txt", "w");
// 判断文件是否成功打开
if (fp == NULL) {
printf("文件打开失败!\n");
return 1;
}
// 向文件写入内容
fprintf(fp, "%s", data);
// 关闭文件
fclose(fp);
printf("输出内容成功导出到output.txt文件中!\n");
return 0;
}
In the code above, the fopen() function is first used to open a file named output.txt with the mode parameter “w” indicating that the file will be opened for writing. If the file does not exist, a new file will be created.
Next, use the fprintf() function to write the content to the file.
Finally, close the file using the fclose() function.
After running the code above, a file named output.txt will be created in the current directory, with “Hello, world!” written in it. Once completed, a message “Output successfully exported to the file output.txt!” will be printed on the terminal.