c - Why is fgets() and/or fputs() removing the spaces from my string? -


i trying receive string of user input , write file. no matter do, however, output has spaces removed string.

i thought whole purpose of using gets()/puts() read/output of characters in string until encounters newline character.

can please tell me doing wrong??

int main (void){     char userinput[100];     char filename[50];     file *cfptr;      printf("enter name of file open: ");     scanf("%s", &filename);      cfptr = fopen(filename, "a+");      printf("enter text add file: \n");     fgets(userinput, 100, stdin);      while (strcmp( userinput, "0") != 0) {         fputs( userinput, cfptr);         fgets(userinput, 100, stdin);     } // end while      fclose( cfptr );      system("pause"); } // end main 

can please tell me doing wrong??

first mistake can notice in scanf, string %s using &:

scanf("%s", &filename);             ^             |             remove it, undefined behavior 

it should just:

scanf("%s", filename); 

your code not work after this. can't find other syntax error believe main bug in code.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

node.js - Node - Passport Auth - Authed Post Route hangs on form submission -

Does Firefox offer AppleScript support to get URL of windows? -