[英]C: Standard input and file input, how to input a string and show new lines in the file
我可能对标准输入和文件输入有一个基本的问题。 这是我的代码:
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("../testFile.txt", "w+");
printf("PLEASE ENTER:\n");
char str[9999];
scanf("%s", str);
fprintf(fp,"%s",str);
fclose(fp);
}
当我尝试输入123\r\n345\r\n567
时,它在 testFile.txt 中显示123\r\n345\r\n567
n567。 显然\r\n
不被视为换行符。 我要它显示,
123
345
567
或包含许多行的类似内容。 谢谢。
您需要循环执行,然后将每个数字分别写入文件。 假设您不知道您将提前插入多少个数字,请先询问用户,将其保存在 int 变量中并在其上运行循环
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.