-2

I am trying to write a program that reads an unknown number of lines of an unknown length (using malloc) and saves them into a structure.

When I try to print what I've saved in the structure, I get a Segmentation fault.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct line{
    struct line* next_line;
    char* data;
}line;

line* read_line(line* ptrLine, FILE* pFile);

int main(int argc, char *argv[])
{
    if(argc < 2){
        return 2;
    }

    FILE* pFile;

    if((pFile = fopen(argv[1], "r")) == NULL){
        return 1;
    }

    line* list_beginning = NULL;
    line* curr_line;

    curr_line = read_line(list_beginning, pFile);

    while(1){
        if(fgetc(pFile) == EOF) break;
        fseek(pFile, -1, SEEK_CUR);
        curr_line = read_line(curr_line, pFile);
    }

    //printf("%c", (list_beginning->data[0]));
    /*while(curr_line->next_line != NULL){
        unsigned int i = 0;
        for(i = 0; i < strlen(curr_line->data); i++){
            printf("%c", curr_line->data[i]);
        }
    }*/

    fclose(pFile);
    return 0;
}

line* read_line(line* ptrLine, FILE* pFile){

    ptrLine = malloc(sizeof(line));

    ptrLine->data = malloc(1024*sizeof(char));
    int curr_position = 0;
    char c;

    while(1){
        c = fgetc(pFile);
        if(c == '\n' || c == EOF) {
            *(ptrLine->data + curr_position) = '\0';
            break;
        }
        *(ptrLine->data + curr_position) = c;
        curr_position++;
    }

    ptrLine->next_line = NULL;
    return ptrLine->next_line;
}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Allemis
  • 47
  • 1
  • 10
  • 1
    Does the code work as expected? – Spikatrix May 20 '15 at 09:00
  • Correct what? What's wrong with your code? – Filipe Gonçalves May 20 '15 at 09:00
  • To elaborate what @CoolGuy said, Please take the [tour](http://stackoverflow.com/tour) and read [How to Ask](http://stackoverflow.com/help/how-to-ask) to learn what we expect from questions. – Sourav Ghosh May 20 '15 at 09:00
  • no - when I try to print what I´ve saved in the structure, I get a Seg fault... – Allemis May 20 '15 at 09:01
  • Please post the full stack trace of your error, it's vital for answering questions. – SuperBiasedMan May 20 '15 at 09:02
  • `c` should be an `int`. – Spikatrix May 20 '15 at 09:03
  • 1
    `ptrLine->next_line = NULL; return ptrLine->next_line;` => `curr_line = read_line(curr_line, pFile);` => `while(curr_line->next_line` : `NULL->next_line` <- Seg fault – BLUEPIXY May 20 '15 at 09:15
  • So let me get this straight... If a file is 4TB, you want to allocate 4TB to copy the bytes from storage to working memory? Coo-coo! This problem comes up often among new programmers, and those of us seasoned programmers realise... The less memory your program requires, the easier it'll be to optimise and the less likely it'll drag the entire system down into hell. Read one line at a time, buddy. In fact, if you can get away with reading ONE CHARACTER at a time, do that instead! Your life will be much easier, because you won't have to worry about allocation to begin with. – autistic May 20 '15 at 12:05

2 Answers2

0
line* read_line(line* ptrLine, FILE* pFile){

ptrLine = malloc(sizeof(line));

ptrLine->data = malloc(1024*sizeof(char));
int curr_position = 0;
char c;

while(1){
    c = fgetc(pFile);
    if(c == '\n' || c == EOF) {
        *(ptrLine->data + curr_position) = '\0';
        break;
    }
    *(ptrLine->data + curr_position) = c;
    curr_position++;
}

ptrLine->next_line = NULL;
return ptrLine->next_line;

}

in this code, the last 2 lines are not good:

  ptrLine->next_line = NULL
  return ptrLine->next_line;

you set next_line to NULL and you return it so your "read_line" function ever return "NULL" to avoid this, change the last line by this:

   return ptrLine;
0

Look at your read_line function. What is the first parameter? I assume you want to build a linked list of line structures, so the first parameter is the 'head' of the linked list. The value of the parameter is on the stack since you are calling a function, but then you overwrite that value by calling malloc.

At the end of your function you then set the next_line field to NULL and return that. That is also not correct and that is the main reason you are getting the SegFault.

The easiest fix is to make a new variable in this function, say new_line, and use that throughout the function. Then at the end you set the next_line field of it to the ptrLine parameter and return new_line.

I am describing the actions more than writing the actual code to make you think about what to do.

bremby
  • 46
  • 5