简体   繁体   中英

Wrong Output C Terminal

I am trying to use Ubuntu terminal to display the users input. If the user enters "exit" the program should quit. If the user enters something other then "/dev/pts/1" it should display "Unable to open for writing". The program keeps printing the else statement no matter what I type in. Help please.

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

Use strcmp() to compare strings:

#include <string.h>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM