-1

I am writing a string to a file. But only the first part of the string is only inserted to file. Others are missing. For eg: If I write "I Like Flowers", "I" is only written into file.

    #include <stdio.h>
int main()
{
        char string[50];
        int marks,i,n;
        FILE *fptr; fptr=(fopen("string.txt","w"));
        if(fptr==NULL){
                printf("Error!");
                return 0;
        }
        printf("Enter a string : \n");
        scanf("%s", string);
        //fprintf(fptr,"%s",string);
        fwrite(string, 1, sizeof(string), fptr);

        fclose(fptr);
        return 0;
}
jay
  • 3,517
  • 5
  • 26
  • 44
  • `fwrite()` works fine – qrdl Dec 13 '13 at 11:21
  • 1
    why this is downvoted? It would be helpful, if you can specify a reason before Downvoting. – jay Dec 13 '13 at 11:21
  • 1
    Programmer should never assume problem with standard library, the problem always with the code you wrote. Well, there is a small, nearly non-existent chance to hit the stdlib bug, but you have to be 500% sure it is not your fault before assuming this. – qrdl Dec 13 '13 at 11:30
  • Sorry. Actually I wrote the question about the bug in my program. Will change the title soon. – jay Dec 13 '13 at 12:28

2 Answers2

6

The scanf() will stop reading at the first space: that is the problem. The scanf("%s") only reads the I from standard input. To read the entire line use fgets() instead. Use strlen() to write only what was read instead of using sizeof(string).

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Thanks. But after adding fgets the program is just exiting before I enter any string. Is this the correct syntax? printf("Enter a string : \n"); fgets(string, 100, fptr); – jay Dec 13 '13 at 11:10
  • `fgets(string, sizeof(string), stdin);`: note `stdin`. _Always_ check return values to ensure success of a function. – hmjd Dec 13 '13 at 11:16
  • Again error. Segmentation fault: 11. Now the program is like this : fgets(string, sizeof(string), stdin); fwrite(string, sizeof(string), strlen(string), fptr); fclose(fptr); – jay Dec 13 '13 at 11:20
  • Read [`fwrite()`](http://en.cppreference.com/w/c/io/fwrite). I meant `strlen()` _instead of_ `sizeof()`. – hmjd Dec 13 '13 at 11:27
  • You don't want to use `sizeof(string)` *and* `strlen(string)`, use `fwrite(string, 1, strlen(string), fptr);` instead. Otherwise you try to write (and thus read from `string`) `sizeof(string)` * `strlen(string)` characters, which is more than is allocated there. – Kninnug Dec 13 '13 at 11:27
  • "The scanf() will stop reading at the first space" mis-leads a bit. `scanf("%s")` scans (and does not save white space), then scans and saves non-white space, then "stop reading at the first space". – chux - Reinstate Monica Dec 13 '13 at 13:31
1

In your code scanf("%s",string) only takes the string up to the first space. To read the total string "I Like Flowers", you have to use the scanf function like this:

scanf("%[^\n]s,string);

it will work.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722