0

I'm supposed to copy fp to lines. I first find the length of the texts in fp then I dynamically allocate lines and retrieve the texts using fgets. I keep getting a "Your return code was -11 but it was supposed to be 0" on my auto grader. This is only part of the code of course. I have a makefile and main. Where is my seg fault??

void read_lines(FILE* fp, char*** lines, int* num_lines){

    int num_chars=0;

    int index=0;

    int lengths[index];

    int i=0;

    //find the length of the rows n cols in fp

    //while there is still character in the text
    while(!feof(fp)){
        //get that character
        char current_char= fgetc(fp);
        //implement the number character
        num_chars++;

        //enter at the end of the first then each line
        if(current_char=='\n'){
            //find the length of the next line of sentence/word.
            // This array stores the length of characters of each line
            lengths[index]= num_chars;
            //update index
            index++;

         // Reset the number of characters for next iteration
           num_chars = 0;
            // Increment the number of lines read so far
            (*num_lines)++;
        }

    }


    //now we need to copy the characters in fp to lines
    (*lines)=(char**) malloc((*num_lines)*sizeof(char*));
    for(i=0;i<*num_lines;i++){
        (*lines)[i]=(char*)malloc(lengths[i]*sizeof(char));
        fgets(*lines[i],(lengths[i]+1),fp);
        fseek(fp,0,SEEK_SET);
        }
    }
Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
Q Mallow
  • 11
  • 5
  • 2
    `int index=0; int lengths[index];` You are (using a `gcc` extension) allocating a zero-byte array. That's going to crash the first time you use it. Have you actually tested your code locally and not through whatever auto-grader tool you have? – Ken Y-N Nov 21 '16 at 08:33
  • 4
    Unrelated to your problem, but you should really read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). Also, the [`fgetc`](http://en.cppreference.com/w/c/io/fgetc) function returns an `int`. – Some programmer dude Nov 21 '16 at 08:34
  • 1
    More related to your problem, negative return codes are indicative of a crash, and you should always solve those using a *debugger*. – Some programmer dude Nov 21 '16 at 08:36

3 Answers3

0

I'm seeing two problems, here.

First, lengths is statically allocated with zero bytes. That can and will never work. You will need to either create a lengths array with a maximum size (say, 256 line maximum) or make lengths a linked list so that it can grow with the index. Alternatively, you can make two passes through the file - once to get the number of lines (after which you allocate your lines array) and once to get the number of characters per line.

Second, although it is a nitpick, you can greatly simplify the code by removing num_lines from your while loop. After of the loop, just set

*num_lines = index;
0

The reason of segfault is your are passing lines pointer in wrong way

fgets(*lines[i],(lengths[i]+1),fp);

correct way is :-

fgets((*lines)[i],(lengths[i]+1),fp);
Sumit Gemini
  • 1,836
  • 1
  • 15
  • 19
0

fix like this

void read_lines(FILE *fp, char ***lines, int *num_lines){
    int num_chars=0;
    /* int index=0; int lengths[index];//lengths[0] is bad. */
    int ch, i = 0, max_length = 0;

    while((ch=fgetc(fp))!=EOF){//while(!feof(fp)){ is bad. Because it loops once more.
        num_chars++;
        if(ch == '\n'){
            ++i;//count line
            if(num_chars > max_length)
                max_length = num_chars;
            //reset
            num_chars = 0;
        }
    }
    if(num_chars != 0)//There is no newline in the last line
        ++i;
    *num_lines = i;

    rewind(fp);//need Need rewind
    char *line = malloc(max_length + 1);
    *lines = malloc(*num_lines * sizeof(char*));
    for(i = 0; i < *num_lines; i++){
        fgets(line, max_length+1, fp);
        (*lines)[i] = malloc(strlen(line)+1);
        strcpy((*lines)[i], line);
    }
    free(line);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70