[英]Wrong Output C Terminal
我正在嘗試使用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);
}
使用strcmp()
比較字符串:
#include <string.h>
if (strcmp(term, "/dev/pts/1") == 0) {
// Strings are equal
}
else {
// Strings are different.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.