0

I am new to C and am trying to read and write structures to a .dat file. When I add the data, I see the characters in the .dat file. However, I am unable to read the data and my code outputs nothing when it should output "val" for every occurrence of a structure.

I have looked at numerous sources but I cannot find how my code differs to those.

https://www.geeksforgeeks.org/readwrite-structure-file-c/ This website was used to initially understand how to do this.

Read/Write to binary files in C I used this to see how my code could be fixed but the solution did not help.

I tried changing the statement in the while loop.

struct person
{
    int id;
    char lastName[15];
    char firstName[15];
    char age[4];
};


int main(void) {
  //create new file
  FILE *fp = fopen("file.dat", "wb+");

  struct person a = {10, "Smith", "John", 25};
  fwrite(&a, sizeof(a), 1, fp);

  struct person b = {2, "Ali", "Jon", 12};
  fwrite(&b, sizeof(b), 1, fp);

 struct person c = {19, "Walter", "Martha", 82};
  fwrite(&c, sizeof(c), 1, fp);

  struct person p; 
  while(fread(&p, sizeof(p), 1, fp))
  printf("val");
}

Currently, it should print 3 "Vals" as there are three persons being added to the dat file. However, nothing is being printed out.

I appreciate the help.

apaul30
  • 3
  • 1
  • 6
    You need to [rewind](https://en.cppreference.com/w/c/io/rewind) the file before you can read it back (or [fseek](https://en.cppreference.com/w/c/io/fseek)). – n. m. could be an AI May 07 '19 at 03:21

2 Answers2

2

When you are done writing the records, the file pointer (the "cursor", the position you are reading/writing) is at the end of the file. You have to set that position back to the begin of the file by using rewind(), fseek() or fsetpos() before trying to read from the file.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
0

If you want to read back and print then try to capture the start position of the file before writing the file with fpos_t and fgetpos() . Later after writing to file set back the initial position using fsetpos() and use fget() to read the content and print them. Check the modified code as below-

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

struct person
{
    int id;
    char lastName[15];
    char firstName[15];
    char age[4];
};


int main(void) {
  //create new file
  FILE *fp = fopen("file.dat", "wb+");
  fpos_t position;


  struct person a = {10, "Smith", "John", "25"};
  fwrite(&a, sizeof(a), 1, fp);

  struct person b = {2, "Ali", "Jon", "12"};
  fwrite(&b, sizeof(b), 1, fp);

  struct person c = {19, "Walter", "Martha", "82"};
  fwrite(&c, sizeof(c), 1, fp);

  fseek(fp, 0, SEEK_SET);

  struct person p; 
  while(fread((void *)&p, sizeof(p),1,fp)==1)
  {
    printf("%d\n",p.id);
    printf("%s\n",p.lastName);
    printf("%s\n",p.firstName);
    printf("%s\n",p.age);    
    printf("\n");
  }
}
Nitin Jadhav
  • 497
  • 1
  • 7
  • 17
  • You have a few typos in your answer and the description at the beginning doesn't match your code. Also, why do you suggest with your code to read a binary file char-by-char with `fgetc()`? – Swordfish May 07 '19 at 11:43
  • @Swordfish. Thanks, corrected the typos. The code is in sync with description- first, get start position using ```fpos_t``` and ```fgetpos()``` later before reading set the position to initial using ```fsetpos()```, as of now modified the statement grammatically to understand well. About ```fgetc()``` its one of the way to read the file, is there any problem or risk in using it? – Nitin Jadhav May 08 '19 at 03:33
  • *About `fgetc()` its one of the way to read the file, is there any problem or risk in using it?* – Risk? No. But I bet apaul30 wants his structs back and not chars. And as the structs contain arrays of char, there is also a good chance that there are strings in the data written to the file, but also garbage after a string when the string doesn't have the exact length of the array. – Swordfish May 08 '19 at 04:00
  • Okay got it. Thank you. – Nitin Jadhav May 08 '19 at 04:03
  • Thanks @Swordfish modified the code to remove ```fgetc()``` – Nitin Jadhav May 08 '19 at 12:23