0

I am trying to read structs from a binary file into array of structs but I keep getting segmentation faults, could anyone help me figure whats wrong with my code? I believe the mistake is how i am getting the strings into the array of structs but I am not sure.

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

#define MAX 150

struct theContacts {
   unsigned long phone;
   long namePos;
   long lastNamePos;
   long emailPos;
   long next;
};
typedef struct theContacts record;

struct livememory {
   char first[MAX];
   char last[MAX];
   char email[MAX];
   unsigned long phone;
};
typedef struct livememory livemem;

int main (void) {

   record *recPtr1;
   livemem strings[1000];
   FILE *fptr;
   long file_end;
   int i;

   fptr = fopen("contacts.db", "a+");
   if (fptr == NULL) {
      printf("Error opening file\n");
      printf("please try again\n");
      exit(0);
   }
   fseek(fptr, 0, SEEK_END);
   file_end = ftell(fptr);

   while(1) {
      recPtr1 = malloc (sizeof(record));
      fread(recPtr1, sizeof(record), 1, fptr);

      fseek(fptr, recPtr1->namePos, SEEK_SET);
      fgets(strings[i].first, MAX, fptr);

      fseek(fptr, recPtr1->lastNamePos, SEEK_SET);
      fgets(strings[i].last, MAX, fptr);

      fseek(fptr, recPtr1->emailPos, SEEK_SET);
      fgets(strings[i].email, MAX, fptr);

      i++;
      fseek(fptr, recPtr1->next, SEEK_SET);
      if (recPtr1->next == file_end) {
         break;
      }
   }
kirkosaur
  • 97
  • 1
  • 1
  • 10
  • SO isn't a debugging service. Compile with symbols, run the code inside a debugger to trace through the program(s) line by line inspecting the values of the relevant variables to learn what is *really* going on. If then a *specific* question arises feel free to come back here. – alk Apr 01 '17 at 15:21
  • I already tried that but all I keep getting is : program received signal SIGSEV, segmentation fault – kirkosaur Apr 01 '17 at 15:24
  • I recommend you start checking for end-of-file. in your loop. For example in the loop condition. (No, [not by using `feof`](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong), check what the first `fread` returns instead.) Then step through the code line by line in a debugger. – Some programmer dude Apr 01 '17 at 15:26
  • When using GDB, and the program ended due to a crash, type `bt` or `bt all` to learn where it crashed, – alk Apr 01 '17 at 15:27
  • Also you might want to *trace* the code inside the debugger. When GDB came up, do `b main`, then `r`, then continue using `s` and/or `f` and/or `n`. Also reading GDB's documentation helps. – alk Apr 01 '17 at 15:29
  • thanks a lot, ill keep that in mind next time before posting. I'll try what you posted and hopefully get whats the problem, its been so frustrating hahaha – kirkosaur Apr 01 '17 at 15:36
  • Also try looking into the file using a so called hex-editor (for the Linux console try `hexdump` using the `-C ` option) . This helps you to understand *what* the code actually is trying to read. – alk Apr 01 '17 at 16:20

0 Answers0