我正在尝试使用 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/