如何读取用户的使用与fgets输入的多条线路,并将其写入使用的fputs在C文件? [英] How to read multiple lines of input from user using fgets and write it into a file using fputs in C?
问题描述
我想读取用户(多线)输入和使用的fputs(其写入到文件)。
I wanted to read input from user (multiple lines) and write it into a file using fputs().
下面是我的code
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char s[25];
fp=fopen("myname","w");
if(fp==NULL)
{
perror("Error opening file\n");
exit(1);
}
while(fgets(s,25,stdin)!=NULL)
fputs(s,fp);
fclose(fp);
return 0;
}
获取用户输入后,我使用Ctrl + C键关闭程序(我使用的Linux)的输入提示。然后,如果我打开该文件,它包含什么。我怎么能解决这个问题?
After getting input from user, i am using Ctrl+C to close the input prompt of the program (I'm using linux). Then if i open the file, it contains nothing. How could I resolve this?
这有什么错的fputs的使用()及与fgets()?
Is there anything wrong with the usage of fputs() & fgets()?
推荐答案
CC
可能杀死过程中,由于缓冲,行不会得到该文件(除非有很多人,他们填补了缓冲区)。
Since C-C
likely kills the process, because of buffering, the lines won't get to the file (unless there's a lot of them and they fill the buffer).
您应该结束与 CD
(UNIX)或 CZ
(DOS),而不是<$ C $输出C> CC 。
You should end the output with C-D
(Unix) or C-Z
(Dos), not C-C
.
作为一个方面说明:与fgets
不会删除换行符和的fputs
增加了自己。不过,我想你要注意的是: - )
As a side note: fgets
doesn't strip the newline and fputs
adds its own. But I guess you were going to notice that :-)
这篇关于如何读取用户的使用与fgets输入的多条线路,并将其写入使用的fputs在C文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!