0

I have a text file with the following data:

11111,First,Last,2,COR,100,SEG,200
22222,F,L,1,COR,100
33333,Name,Name,3,COR,100,SEG,200,CMP,300
***

I need to separate the data in each line (at the commas) and add each line of data into a linked list. Here is a piece of the code that I am using to do it:

struct courseInfo{
    int courseID;
    char courseName[30];
};
typedef struct courseInfo cInf;

struct studentInfo{
    char studentID[8];
    char firstN[20];
    char lastN[25];
    int nCourses;
    cInf courseInf[10];
    struct studentInfo *next;
};
typedef struct studentInfo sInf;
void loadStudentInfo(){
FILE *loadFile;
loadFile = fopen("studentRecords.txt", "r");
while(1){
    sInf *loadStudent;
    loadStudent = (sInf*)malloc(sizeof(sInf));
    char Temp[256];
    fgets(Temp,256,loadFile);
    if (Temp[0] == '*')
        break;
    int commaCount = 0;
    int i = 0;
    while(Temp[i] != '\0'){
        if(Temp[i] == ',' && commaCount == 0){
            char stdID[8];
            strcpy(stdID, Temp);
            int commaLoc = 0;
            while(stdID[commaLoc] != ','){                                             //Finds where stdID ends
                commaLoc++;
            }
            for(;commaLoc < 8; commaLoc++){                                            //Delimits stdID, 8 is the max size of studentID
                stdID[commaLoc] = '\0';
            }
            printf("%s\n", stdID);
            strcpy(loadStudent ->studentID, stdID);    //Causing segmentation fault
            commaCount++;
        }

Now, what I have done to separate the data in each line may not be the most efficient but I tried using strtok() (which read the first line fine but resulted in a segmentation fault when reading the second line) and also tried using fscanf and %[^,] but it did not work for me. Anyway, I don't think that the method I am using to separate the data is causing the segmentation fault. This loop will continue on separating and storing the rest of the data in the appropriate member of loadStudent. The line causing the segmentation fault is shown above. Any help with determining the cause of this error is appreciated.

user7438591
  • 29
  • 1
  • 6
  • I think you left off a `typedef struct studentInfo sInf;` – lockcmpxchg8b Aug 22 '18 at 02:47
  • Also, this program can't segfault. It's sitting in an infinite loop because your `while` condition is always true, but the condition of the `if` inside it is always false. Hint: `i` never increments. – lockcmpxchg8b Aug 22 '18 at 02:50
  • Yeah as I said earlier, I only put the piece that is giving me the seg fault. I have i incrementing below that but I didn't copy it. – user7438591 Aug 22 '18 at 02:51
  • Assume that there is not an infinite loop (because there isn't), and that i is incrementing (because it is). – user7438591 Aug 22 '18 at 02:52
  • 2
    Isn't `strcpy(stdID, Temp);` going to copy ~35 characters (i.e., the whole first line of your file) over the 8-character wide buffer `stdId`? That could be enough to corrupt other variables on the stack and give you random behaviors. – lockcmpxchg8b Aug 22 '18 at 03:00
  • I bet if you `printf("LoadStudent: 0x%p\n", loadStudent);` right after the allocation, then again right before the segfaulting line, you'll see it change. (Assuming the `printf` you've got right before that line is giving you what you expect). – lockcmpxchg8b Aug 22 '18 at 03:07
  • All IDs are 6 characters long – user7438591 Aug 22 '18 at 03:13

1 Answers1

0

Your problem starts here:

strcpy(stdID, Temp);

This will copy the whole first line into stID. As stID can only hold 8 chars and the first line is more than 8 chars, you write out of bounds which is undefined behavior. After that anything may happen.

You write that you couldn't make strtok work. Try this:

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

int main(void) {
    char str[] = "11111,First,Last,2,COR,100,SEG,200";
    char* p = strtok(str, ",");
    while(p)
    {
        printf("%s\n", p);
        p = strtok(NULL, ",");
    }
    return 0;
}

Output:

11111
First
Last
2
COR
100
SEG
200
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • I had strtok work for the first line but it would segfault when looping to get the second line. I'm going to try expanding the size of stdID right now and I'll get back to you. Thanks – user7438591 Aug 22 '18 at 03:29
  • I still get the segmentation fault when expanding the size of stdID – user7438591 Aug 22 '18 at 03:32
  • @user7438591 How did you change `stdID` ? New size ? – Support Ukraine Aug 22 '18 at 03:36
  • Yeah just changed the size to 256 – user7438591 Aug 22 '18 at 03:37
  • I also just tried using strtok again (because I had the code saved from before) but this time, all of the strtok calls were in a different function that was then called by the above function and that also resulted in a seg fault – user7438591 Aug 22 '18 at 03:39
  • After increasing the size I can't reproduce your problem. See https://ideone.com/9snYAu The problem may be in some code you didn't post – Support Ukraine Aug 22 '18 at 03:46
  • You are correct, there is an error in a different part of my code. It was the size of the strings that I was copying too. Thank you! – user7438591 Aug 22 '18 at 03:51