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
6
  • 1
    have you tried a debugger? Apr 11, 2013 at 19:06
  • 1
    @FredrikPihl no, let me know , which to use and how ?
    – Kajal
    Apr 11, 2013 at 19:07
  • 2
    if you're on a linux system, I'd recommend gdb Apr 11, 2013 at 19:07
  • 1
    @Kajal - gdb
    – Mike
    Apr 11, 2013 at 19:08
  • 2
    @Kajal before to go for debug I think you should read buffer overflow You code is buggy its easy to find..Undefined Behavior. Apr 11, 2013 at 19:16

4 Answers 4

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)

9
  • 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, 2013 at 19:09
  • @KingsIndian how can i do it with char *line; , not changed by an array
    – Kajal
    Apr 11, 2013 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, 2013 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, 2013 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, 2013 at 19:15
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;
}
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.

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.

1
  • can you explain in detail , then how can i fix
    – Kajal
    Apr 11, 2013 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.