scanf(“%[^\n]”, s);那么如何在C中strcmp呢?
scanf(" %[^\n]", in);
例如,我输入 Knock Knock 并按 Enter 键,
但我里面的代码块
if (strcmp ("Knock Knock",out)==0)
不起作用,
请指导我,非常感谢!
char in[80],out[80];
void input(){
printf("Client: ");
scanf("%[^\n]",in);
fp=fopen("test","w");
if (!fp) return ;
fputs(in,fp);
fclose(fp);
}
fp=fopen("test","r");
fgets(out,81,fp);
fclose(fp);
fp=fopen("test","w");
if (strcmp ("Knock Knock",out)==0)
fputs("Server: Who is there?\n",fp);
scanf(" %[^\n]", in);
then for example , i input Knock Knock and hit enter
but my code block inside
if (strcmp ("Knock Knock",out)==0)
does not work
please instruct me ,thanks a lot!
char in[80],out[80];
void input(){
printf("Client: ");
scanf("%[^\n]",in);
fp=fopen("test","w");
if (!fp) return ;
fputs(in,fp);
fclose(fp);
}
fp=fopen("test","r");
fgets(out,81,fp);
fclose(fp);
fp=fopen("test","w");
if (strcmp ("Knock Knock",out)==0)
fputs("Server: Who is there?\n",fp);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,代码的布局非常混乱,而且就目前情况而言,它永远无法编译。您有一个似乎从未调用过的函数
input()
,并且您将代码留在了应该位于另一个函数内的函数之外,或者更好的是,所有代码都应该包含在内main()
函数,以便可以执行它。这是您想要做的事情的清理示例:一些重要注意事项:
1)
fp
有一个文件类型FILE*
,因为这是返回fopen()
,但您从未如此声明它。所以编译时永远不会出现该错误。2) 每次使用
w
标志打开文件时,它都会删除该文件的全部内容。因此,如果您打算附加到文件以获取程序输出的历史记录,则需要在调用fopen()
时使用a+
标志3 )如果您无法打开文件,最好能打印出某种类型的错误,而不是在程序从
stdin
获取输入后绞尽脑汁地思考为什么“test.txt”为空。 。另外,如果您要继续重新打开文件,请每次检查 NULL,因为尝试使用 NULL 文件指针会得到不可预测的结果(很可能会导致崩溃)。4)
scanf()
可能会导致用户输入(或恶意用户输入)导致严重的缓冲区溢出...将fgets()
与stdin< 结合使用/code> 改为已知长度的缓冲区。
您现在应该能够编译并运行此代码。我在 Ubuntu 上使用 gcc 4.4.3 工作。运行后,您的“test.txt”文件应如下所示:
First off, the layout of the code is very confusing, and as it stands, it would never compile. You have a function
input()
that you never seem to call, and you leave code outside the function that should be inside another function, or better yet, all of it should be contained inside amain()
function so that it can be executed. Here is a cleaned up example for what you're wanting to-do:Some important notes:
1)
fp
has a file-typeFILE*
, since that is the return offopen()
, but you never declare it as such. So this would never compile with that error.2) Every time you open a file with the
w
flag, it erases the entire contents of the file. So if you were intending on appending to the file to have a history of what your output from your program was, you need to use thea+
flag when callingfopen()
3) It would be nice to have some type of error print-out if you failed to open the file rather than scratching your head at why "test.txt" is empty after the program takes the input from
stdin
. Also if you're going to keep re-opening the file, check for a NULL each time since you're going to get unpredictable results from trying to work with a NULL file pointer (most likely a crash).4)
scanf()
can result in nasty buffer over-runs from user-input (or malicious user input) ... usefgets()
withstdin
to a known-length buffer instead.You should be able to compile this code now and run it. Works for me with gcc 4.4.3 on Ubuntu. After running, your "test.txt" file should look like: