0

My other functions work fine but once I call this readfile funtions, I have an exit code/core dump. Temp is pointer pointing to a double pointer (the linked list itself). I have a feeling the error is somewhere in the first 6 lines because I am positive everything else is correct.

I have tried to change around my strategy in of the temp->next and how I move the pointer throughout the list but nothing seems to work

while ( !feof(fp) && return_val == 0 ){
            if ( temp != NULL ){
                temp = malloc(sizeof(struct record));
                temp->next = next;
                next->next = NULL;
                temp = temp->next;
            }
            fscanf(fp, "%*s %*s %d",&temp->accountno);
            track = 0;
            while ( track == 0 ){
                ch = fgetc(fp);
                if ( ch == '<'){
                    ch = fgetc(fp);
                    while ( ch != '>' ){
                        temp->name[ni] = ch;
                        ch = fgetc(fp);
                        ni++;
                    }
                    temp->name[ni] = '\0';
                } else if ( ch == '['){
                        ch = fgetc(fp);
                        while ( ch != ']' ){
                            temp->address[ai] = ch;
                            ch = fgetc(fp);
                            ai++;
                        }
                        temp->address[ai] = '\0';
                        track = 1;
                }
            }
            ni = 0;
            ai = 0;
        }

I expect the program to simply read the file, the function takes in a double pointer and a string array(name of the file) and it is supposed to read from the file, the account number, name, and address and store it in the linked list.

Steve Friedl
  • 3,929
  • 1
  • 23
  • 30
  • 2
    Not an answer, but do read [Why is `while(!feof(file))` always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – John Bollinger Nov 03 '19 at 12:57
  • I tend to agree that the problem is likely to be in the first few lines, because I'm having trouble figuring out what they're supposed to do, or why you want to do that only in the event that `temp` is initially **non**-null. It sort of looks like you're trying to prepend a new node to the head of the list, but it also sort of looks like you're trying to append one to the tail of the list. Note in particular that if `temp` is initially null then you will get a null-pointer dereference when the `fscanf` arguments are evaluated. – John Bollinger Nov 03 '19 at 13:03
  • 1
    If temp==NULL, you're doing strange things – JohanC Nov 03 '19 at 13:46
  • 2
    Did you mean if ( temp == NULL ) ? That would seem more reasonable than what you have now. – Avi Berger Nov 03 '19 at 16:27
  • when asking a question about a run-time problem, as this question is doing, please post a [mcve] so we can reproduce the problem and help you debug it. – user3629249 Nov 03 '19 at 17:26
  • 1
    OT: regarding: `temp = malloc(sizeof(struct record));` Always check (!=NULL) the returned value to assure the operation was successful. If not successful, then call `perror( "malloc failed" );` to output both you error message and the text reason the system thinks the error occurred to `stderr`. – user3629249 Nov 03 '19 at 17:29
  • what is the `struct` definition of the fields in each entry in the linked list? – user3629249 Nov 03 '19 at 17:30
  • what is contained in the variable: `next`? – user3629249 Nov 03 '19 at 17:31
  • regarding: `while ( ch != '>' ){ temp->name[ni] = ch; ch = fgetc(fp); ni++; } temp->name[ni] = '\0';` What if there is not enough room in the `name[]` array? – user3629249 Nov 03 '19 at 17:35
  • where is `return_val` set? – user3629249 Nov 03 '19 at 17:37
  • regarding: `read the file, the function takes in a double pointer and a string array(name of the file) and it is supposed to read from the file, the account number, name, and address and store it in the linked list.` What does a typical line in the file look like? – user3629249 Nov 03 '19 at 17:39
  • 1
    regarding: `fscanf(fp, "%*s %*s %d",&temp->accountno)` 1) What if those first two strings contain spaces? 2) when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. in the current scenario, any returned value other than 1 indicates an error occurred – user3629249 Nov 03 '19 at 17:43

0 Answers0