scanf(“%[^\n]”, s);那么如何在C中strcmp呢?

发布于 2024-11-07 08:00:07 字数 562 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-11-14 08:00:07

首先,代码的布局非常混乱,而且就目前情况而言,它永远无法编译。您有一个似乎从未调用过的函数 input() ,并且您将代码留在了应该位于另一个函数内的函数之外,或者更好的是,所有代码都应该包含在 内main() 函数,以便可以执行它。这是您想要做的事情的清理示例:

#include <stdio.h>

char in[80],out[80];

int main()
{
    printf("Client: ");
    scanf("%[^\n]",in);  //you really should use fgets() here

    FILE* fp = fopen("test.txt","w");
    if (!fp)
    {
        perror("Failed to open file");
        return 1;
    }

    fputs(in,fp);
    fputs("\n",fp);
    fclose(fp);


    fp = fopen("test.txt","r");
    if (!fp)
    {
        perror("Failed to open file");
        return 1;
    }

    fgets(out,80,fp);
    fclose(fp);

    fp = fopen("test.txt","a+");
    if (!fp)
    {
        perror("Failed to open file");
        return 1;
    }

    if (strcmp ("Knock Knock\n",out)==0)
        fputs("Server: Who is there?\n",fp);

    return 0;
}

一些重要注意事项:

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”文件应如下所示:

Knock Knock
Server: Who is there?

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 a main() function so that it can be executed. Here is a cleaned up example for what you're wanting to-do:

#include <stdio.h>

char in[80],out[80];

int main()
{
    printf("Client: ");
    scanf("%[^\n]",in);  //you really should use fgets() here

    FILE* fp = fopen("test.txt","w");
    if (!fp)
    {
        perror("Failed to open file");
        return 1;
    }

    fputs(in,fp);
    fputs("\n",fp);
    fclose(fp);


    fp = fopen("test.txt","r");
    if (!fp)
    {
        perror("Failed to open file");
        return 1;
    }

    fgets(out,80,fp);
    fclose(fp);

    fp = fopen("test.txt","a+");
    if (!fp)
    {
        perror("Failed to open file");
        return 1;
    }

    if (strcmp ("Knock Knock\n",out)==0)
        fputs("Server: Who is there?\n",fp);

    return 0;
}

Some important notes:

1) fp has a file-type FILE*, since that is the return of fopen(), 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 the a+ flag when calling fopen()

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) ... use fgets() with stdin 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:

Knock Knock
Server: Who is there?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文