2015-04-27 106 views
1

我想用Ubuntu终端来显示用户输入。如果用户输入“退出”,程序应该退出。如果用户输入了“/ dev/pts/1”以外的内容,它应该显示“无法打开进行写入”。无论我输入什么内容,该程序都会继续打印else语句,请帮助。错误的输出C终端

#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); 
} 
+0

你试过'strcmp'? – Mephy

+0

可能重复的[我如何正确比较C中的字符串?](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – ericbn

+0

是的工作!感谢您的快速回复 –

回答

1

使用strcmp()比较字符串:

#include <string.h> 

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

这工作谢谢!没想到用那个 –