0

I am writing a function that takes a file pointer and deletes a few lines from it. The file is of the form

student1_ID
firstName
lastName
numCourses
course1
course2
...
student2_ID
...

The method I'm using to solve this is to take user input on what student I would like to delete. I then find that student's ID, ignore the next three lines until I get to the number of courses for that student. Convert that line to a number, and read in that number of lines. Otherwise, copy every other line to the new file. At the end, I return a pointer to the new file.

However, I'm getting a segfault for some reason.

Here's my code:

//This function delets lines from a file that are associated with a certain student
FILE *deleteStudent(FILE *fptr){
    FILE *fptr2;                        //Pointer to a new file
    int i = 0;
    char c[40], line[40], s = '\n';

    printf("Which stduent would you like to delete? \n");
    while((c[i] = getchar()) != s){                 
        i++;
    }

    while(!feof(fptr)){                     //While not at end of file
        fgets(line, 40, fptr);              //Get a line
        if(strcmp(line, c) == 0){           //If it matches user input, keep reading until next student
            for(int i = 0; i < 3; i++){     //Reads student ID, first name, last name, # of courses
                fptr2 = fopen("studentlist.txt", "r");
            }
            int numcrs = (int)line[0];      
            for(int i = 0; i < numcrs; i++){    //Reads through student's list of courses
                fptr2 = fopen("studentlist.txt", "r");
            }
        }
        else{                               //Otherwise, copy the line to the new file
            fptr2 = fopen("newfile.dat", "a");
            fprintf(fptr2, "%s", line);
            fclose(fptr2);
        }
    }
    return fptr2;                           //Return the a pointer to the new file
}

The output is as follows

$ ./my.prog
Which stduent would you like to delete?
111111111
Segmentation fault (core dumped)

I would appreciate the help.

Thanks

The_Questioner
  • 240
  • 2
  • 7
  • 17
  • You have to null-terminate the string `c` after reading the student to be deleted. Otherwise `strcmp` will run past the end of the array trying to find the end of the string. – Jim Lewis Mar 27 '17 at 19:27
  • You do not write a nul terminator to `char c[40]` before passing it to `strcmp`, and probably other errors too. Anyway, the final `newline` is retained by `fgets` but rejected by your loop. – Weather Vane Mar 27 '17 at 19:27
  • @JimLewis, Thanks. After the first while loop, I added c[i++] = 0; That should address that problem. However, I'm still getting the same segfault error and the same output. – The_Questioner Mar 27 '17 at 19:34
  • @WeatherVane. I made the change as indicated in my above comment. Though, I'm still getting the same error. – The_Questioner Mar 27 '17 at 19:45
  • 1
    [why `while (!feof(fptr))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Mar 27 '17 at 19:48
  • Why are you opening `studentlist.txt` in a loop and not doing anything with it? – Barmar Mar 27 '17 at 19:50
  • @The_Questioner Did you remove the newline from `c` before comparing? Why don't you just use `fgets(c, 40, stdin)` instead of your loop? – Barmar Mar 27 '17 at 19:53
  • @The_Questioner you did not pay attention to my comment about `newline`. – Weather Vane Mar 27 '17 at 19:53
  • It seems like you think that `fptr2 = fopen("studentlist.txt", "r");` reads the next line from the file. That's the only explanation I can find for the way you use it in your loops. – Barmar Mar 27 '17 at 19:55
  • What is `int numcrs = (int)line[0];` supposed to do? `line[0]` is the first character of the user ID. So you're setting `numcrs` to the ASCII code for that character, and then using that as a loop limit. E.g. if the user ID is `11111` it sets `numcrs = 33`. – Barmar Mar 27 '17 at 19:57
  • What I'm trying to do, is read in lines that I want to delete, and just not do anythign with them. . Then read the next line, ,etc until I get to the new student information. Then, I would like to copy that info into the new file. That is what the else statement is for. – The_Questioner Mar 27 '17 at 20:19

0 Answers0