c - 输出 C 端错误

标签 c printing terminal

我正在尝试使用 Ubuntu 终端来显示用户输入。如果用户输入“exit”,程序应该退出。如果用户输入的不是“/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);
}

最佳答案

使用strcmp()比较字符串:

#include <string.h>

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

关于c - 输出 C 端错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29906656/

相关文章:

c - 如果 arr 是 T 型数组,那么 &arr 的用途是什么?

ruby 打印按钮

google-maps - 在 WebKit 中打印带有多个嵌入式 Google map 的页面会产生图 block 错误

python - 从终端杀死linux中的python解释器

c - function() 中未指定的参数会发生什么?

c - 编写二进制文件的名称,避免在 C 中使用 sprintf()

c - 如何将参数的值包含到 makefile 中的字符串中

c# - 根据元素的 x,y 位置打印列表内容(离散整数)

java - 从 java 运行一个 .sh 脚本

console - 如何从终端使用比特币私钥签署消息