【问题标题】:Wrong Output C Terminal输出 C 端子错误
【发布时间】:2015-04-27 21:53:42
【问题描述】:

我正在尝试使用 Ubuntu 终端来显示用户输入。如果用户输入“退出”,程序应该退出。如果用户输入“/dev/pts/1”以外的内容,则应显示“无法打开写入”。无论我输入什么,程序都会继续打印 else 语句。请帮助。

#include <stdio.h>

main()
{
FILE *fpt;  
char str[100];
char term[20];
fpt = fopen("/dev/pts/1", "w");

while(1)
{
    printf("Enter the terminal to display in: ");
    scanf("%s", term);
    if(term != "exit");
    {
        if(term == "/dev/pts/1")
        {           
            printf("Enter the text to display: ");
            scanf("%s", str);
            fprintf(fpt,"%s\n", str);       
        }
        else
        {
            printf("Unable to open %s for writing\n", term);
        }
    }
}   

fclose(fpt);
}

【问题讨论】:

标签: c printing terminal


【解决方案1】:

使用strcmp()比较字符串:

#include <string.h>

if (strcmp(term, "/dev/pts/1") == 0) {
    // Strings are equal
}
else {
    // Strings are different.
}

【讨论】:

    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2017-08-02
    • 2018-06-06
    相关资源
    最近更新 更多