1

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);
}
Tevin Mosley
  • 27
  • 3
  • 6

1 Answers1

1

Use strcmp() to compare strings:

#include <string.h>

if (strcmp(term, "/dev/pts/1") == 0) {
    // Strings are equal
}
else {
    // Strings are different.
}
Oswald
  • 31,254
  • 3
  • 43
  • 68