c - User input to break while loop to return to the main -


so, need break loop user input if c, not break loop , return main.

it sticks same loop.

void createfile(void) {     file *newfile;     char *file_name = malloc(sizeof(*file_name));     printf("\nenter name of file want create:\n");     scanf("%s",file_name);      while(access(file_name, 0) != -1)//in case file exists         {         printf("\n%s file exists\nplease re_enter file name or c go main menu.\n\n", file_name);         scanf("%s", file_name);         if ( file_name == 'c')         {             return;// returning main menu         }         }     if(access(file_name, 0) == -1)//if not exist     {         newfile = fopen(file_name,"w+");         fclose(newfile);         printf("\nfile has been created successfully! :d\n\nplease enter r return main menu ");         scanf("%s",file_name);         if (file_name == 'r')         {             return;         }     }     remove("c"); // in case unwanted c file created.     remove("r"); } 

file_name pointer character. (it's pointer first character of nul-terminated sequence of characters, still pointer character.) that's evident declaration:

char *file_name; 

on other hand, 'c' integer. likely, integer 67, ascii code letter c.

so comparing pointer -- is, address -- integer.

in c that's legal, meaningful if integer comparing pointer 0. compiler should warn this.

the end result comparison file_name == 'c' evaluating 0 (false).

what intended compare string pointed file_name string literal "c", follows:

if (strcmp(file_name, "c") == 0)) 

strcmp standard library function compares 2 character strings (given pointers respective initial characters) , returns negative integer if first 1 comes first (alphabetically), positive integer if second 1 comes first, , 0 if 2 strings equal.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -