1

This program work,
It reads a big log file line by line , After reading 3 lines , it shows segmentation fault .

int main(int argc, char *argv[])
{
  char *line;
  FILE *my_stream;
  char *my_filename = "log";
  my_stream = fopen (my_filename, "r");
  while(fscanf (my_stream, "%s", &line)!= EOF)
  {
  printf ("==> %s\n", &line);
  }
  fclose (my_stream);   
  return 0;
 } 

OUTPUT

==> 123    ==> 12345    ==> 1234568 Segmentation fault
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Kajal
  • 223
  • 4
  • 15

4 Answers4

6

You haven't allocated memory for line. Either declare it as:

char line[256];

Or do an malloc for it.

Note: you don't need & neither in scanf nor in printf if you are dealing with a string (%s format specifier)

P.P
  • 117,907
  • 20
  • 175
  • 238
  • 1+. Also, `line` is already a pointer. Doing `&line` will get the "pointer to the pointer", what is wrong in the both cases it is being used. – Havenard Apr 11 '13 at 19:09
  • @KingsIndian how can i do it with char *line; , not changed by an array – Kajal Apr 11 '13 at 19:10
  • Also don't forget to check the return value of fopen, the value of `my_stream` in this case, to make sure you can actually read the file. – Kninnug Apr 11 '13 at 19:11
  • @Kajal Then you have to allocate memory using `malloc` (or calloc). do : `line = malloc(256);` before using `line`. i.e. right before the while loop. Note `256` is just number I used which you can change as per your needs. – P.P Apr 11 '13 at 19:13
  • @KingsIndian char *line; FILE *my_stream; char *my_filename = "pwd.lst"; my_stream = fopen (my_filename, "r"); line = malloc(256); while(fscanf (my_stream, "%s", &line)!= EOF) { printf ("==> %s\n", &line); } fclose (my_stream); changed but still the same error – Kajal Apr 11 '13 at 19:15
  • @Kajal Remove the `&` from fscanf() and printf() as noted in the answer and also mentioned by Havenard. – P.P Apr 11 '13 at 19:17
  • @Havenard, &line will get the "pointer to the pointer", let me know how , & line will get address of that pointer , is it again a pointer ?? – Kajal Apr 11 '13 at 19:20
  • Yes, but its a pointer to the pointer, not the pointer to the data. `line` is type `char*`, `&line` is type `char**`. – Havenard Apr 11 '13 at 19:21
  • @Kajal `line` is a pointer to char. If you take `&line` then you get a pointer to a pointer whereas fscanf() and printf expects a pointer when you specify `%s` format specifier. So you are passing wrong pointers to both functions which is why you get crash. – P.P Apr 11 '13 at 19:23
3

You need to allocate space for line either on the stack or the heap. Also do not pass the address of line to fscanf and printf.

int main(int argc, char *argv[])
{
    char line[256];
    FILE *my_stream;
    char *my_filename = "log";
    my_stream = fopen (my_filename, "r");
    while(fscanf (my_stream, "%255s", line)!= EOF)
    {
        printf ("==> %s\n", line);
    }
    fclose (my_stream);

    return 0;
}
Joe
  • 56,979
  • 9
  • 128
  • 135
2

You have not allocated any space at all for the line. fscanf is thus writing your logfile's lines into memory at some random location and clobbering whatever happens to be there. You get lucky three times and then it blows up.

For this task you should ideally be using getline. If you don't have that, fgets will do, but you will need to allocate it some space. Think char linebuf[SOME LARGE NUMBER].

Never use *scanf.

Community
  • 1
  • 1
zwol
  • 135,547
  • 38
  • 252
  • 361
2

In the line char *line you allocate space for one pointer to a char. In your fscanf statement you read whole lines of text into that address. You never allocate any space for the text that you read with fscanf, so you overwrite lots of memory that is used for other things.

Mikkel
  • 3,284
  • 2
  • 18
  • 20