0

I am trying to receive a string of user input and write it to a file. No matter what I do, however, the output always has the spaces removed from the string.

I thought that the whole purpose of using gets()/puts() was that it would read/output all of the characters in a string until it encounters a newline character.

Can somebody please tell me what I am doing wrong??

int main (void){
    char userInput[100];
    char filename[50];
    FILE *cfPtr;

    printf("Enter name of file to open: ");
    scanf("%s", &filename);

    cfPtr = fopen(filename, "a+");

    printf("Enter text to add to 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
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
tbutman
  • 165
  • 2
  • 8
  • "0" is not a newline character. It is "0". And you are comparing a whole string to "0" not just a character. Maybe you mean fgetc? – Jiminion Jul 23 '13 at 19:26
  • Sorry, I should have explained that bit. I am checking the string against '0' so that the program will exit if the user enters zero. Otherwise, anything other than 0 will get written to the text file. – tbutman Jul 23 '13 at 19:35
  • I still don't think the strcmp will work, unless the user is to put a single "0" to end things? – Jiminion Jul 23 '13 at 19:39
  • Does userinput need to be cleared or flushed? – Jiminion Jul 23 '13 at 19:40
  • Correct, the user is to enter a single "0" to end. – tbutman Jul 23 '13 at 19:46
  • I tried adding an fflush(stdin) before the fgets statement but unfortunately the problem still remains :( – tbutman Jul 23 '13 at 19:52
  • @user2611922 `fflush(stdin);` is undefined behavior. Please don't to this [Read-Answer](http://stackoverflow.com/questions/16752759/fflush-doesnt-work/16752791#16752791) – Grijesh Chauhan Jul 23 '13 at 20:33

3 Answers3

3

Can somebody please tell me what I am doing wrong??

First mistake that I can notice is in scanf, for string with %s you are using &:

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

it should be just:

scanf("%s", filename);

your code will not work after this. I can't find any other syntax error so I believe this is the main bug in your code.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • It's a very good and correct pont with the UB, OP shouldn't even be looking further before fixing this bug. However, I don't see any reason why it wouldn't work :) –  Jul 23 '13 at 19:24
  • @H2CO3 Didn't get :( :( *`I don't see any reason why it wouldn't work`* ? do I need to improve/change answer ? – Grijesh Chauhan Jul 23 '13 at 19:30
  • 1
    No, you don't. I'm just pointing out that although it's pointless to try to reason about UB, I would *still* expect this particular case to work :) –  Jul 23 '13 at 19:31
  • I agree with Mr. Acid, the point is valid, but I don't think that's the problem. Compilers breeze over this sort of thing. – Jiminion Jul 23 '13 at 19:38
  • Also, just to clarify, even though I left the ampersand in my first scanf statement, isn't that technically okay since the name of an array points to the location of the first element (aren't &array, array, &array[0] all equal to the location in memory of the start of the array)? – tbutman Jul 23 '13 at 19:45
  • @user2611922 **No**, read: [What does sizeof(&arr) return?](http://stackoverflow.com/questions/15177420/what-does-sizeofarr-return/15177499#15177499) – Grijesh Chauhan Jul 23 '13 at 19:46
  • @user2611922 that is the reason I didn't explained further. read last like and comments by H2CO3. – Grijesh Chauhan Jul 23 '13 at 19:53
  • 1
    Gotcha. Regardless, thanks for the link, Grijesh. Being very new to programming I think that will be very informative. – tbutman Jul 23 '13 at 19:57
  • But this doesn't answer the question. – Jiminion Jul 23 '13 at 20:01
  • @Jim why? not it answers, Suggest me if I am missing or you may like to post your own answer... – Grijesh Chauhan Jul 23 '13 at 20:02
  • I still don't think the ampersand matters in this case when using an array with the scanf function, since the function expects a memory address. In this case &array, array, &array[0] all point to the same address in memory so this shouldn't have any ill effects on the code, no? – tbutman Jul 23 '13 at 20:06
  • @user2611922 ok I will answer you, first try run your code without `&` then let me know what happened and what you wants – Grijesh Chauhan Jul 23 '13 at 20:08
  • It doesn't answer the question of why his spaces are removed. Hey, user2611922, can you show input and how it changes when put in the file? – Jiminion Jul 23 '13 at 20:11
  • 1
    @Grijesh On my machine the code performs identically with and without the ampersand. However, neither way fixes my problem with removing the spaces but at least I know my code works on other machines. – tbutman Jul 23 '13 at 20:14
  • @user2611922 I take a look on your code again, I didn't give attention as I found its under Undefined behavior. – Grijesh Chauhan Jul 23 '13 at 20:15
  • @Jim *`It doesn't answer the question`* doesn't works, either suggest improvement or post your own answer. – Grijesh Chauhan Jul 23 '13 at 20:16
  • @user2611922 do you enter `0` to terminate while-loop ? – Grijesh Chauhan Jul 23 '13 at 20:23
  • @Jim If I enter the text string "this is a string" into the console, the resultant text file will have "thisisastring". – tbutman Jul 23 '13 at 20:29
  • @Grijesh, yes, 0 to terminate. So far it sounds like this problem is specific to my machine :( – tbutman Jul 23 '13 at 20:31
  • @user2611922 `1.` initialized array e.g. `char userInput[100] = {0};` `2.` open file in **`w`** mode not `a+` read differences about it. `3` in next attempt -- Delete previously created file id you use `a` mode. – Grijesh Chauhan Jul 23 '13 at 20:38
1

1) fgets() and fputs() won't remove any spaces from your string.

2) One potential "gotcha" is that fgets() WILL retain the "Enter" character(s), which you probably don't consider part of your string.

3) The problem in your code is probably with your "scanf()" - you should pass "filename" (which is an array), instead of "&filename" (which is duplicate redundant).

4) Even better, you might consider substituting "fgets()" for "scanf()".

IMHO...

PS: Here's a great link on using fgets() and friends:

http://beej.us/guide/bgc/output/html/multipage/gets.html

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks everyone for the feedback but it appears there may be something wrong with my compiler because I am still experiencing the same problem with having it remove the spaces. If I enter the string "add this string", it writes the string "addthisstring" to the file. So I'm not sure what is going on. Can someone please try running the code on their machine to see if they can get it to work correctly? – tbutman Jul 23 '13 at 19:40
  • Works for me. Sort of. had to make compare "0\n" to account for new line. Spaces seem to be conserved. system("pause") did not work. What OS are you on? – Jiminion Jul 23 '13 at 19:52
  • I'm on Windows 7. Thanks for confirming that it works though!! I have been messing with this code for over an hour, trying every combination of fgets/gets/scanf and the corresponding print/puts functions trying to get it to work. At least now I know it's not the code. Maybe it has something to do with my compiler or project on Visual Studio?? I'll keep messing around with it. Thanks, again! – tbutman Jul 23 '13 at 20:01
  • Visual C++ runs a bit different that g++. There are some differences. I'd add "%s\n" for the filename grab too. It seems to stick an extra newline into the output without it. – Jiminion Jul 23 '13 at 20:10
0

This runs under g++ and linux.

int main (void){  
  char userInput[100];  
  char filename[50];  
  FILE *cfPtr;

  printf("Enter name of file to open: ");  
  scanf("%s\n", filename);

  cfPtr = fopen(filename, "a+");

  printf("Enter text to add to file: \n");  
  fgets(userInput, 100, stdin);

  while (strcmp( userInput, "0\n") != 0) {  
      fputs( userInput, cfPtr);  
      fgets(userInput, 100, stdin);  
  } // end while

  fclose( cfPtr );

  //system("pause");  
} // end main
Jiminion
  • 5,080
  • 1
  • 31
  • 54