39

Is it possible to write an entire struct to a file

example:

struct date {
    char day[80];
    int month;
    int year;
};
john-jones
  • 7,490
  • 18
  • 53
  • 86
amarVashishth
  • 847
  • 3
  • 12
  • 26

1 Answers1

45

Is it possible to write an entire struct to a file

Your question is actually writing struct instances into file.

  1. You can use fwrite function to achieve this.
  2. You need to pass the reference in first argument.
  3. sizeof each object in the second argument
  4. Number of such objects to write in 3rd argument.
  5. File pointer in 4th argument.
  6. Don't forget to open the file in binary mode.
  7. You can read objects from file using fread.
  8. Careful with endianness when you are writing/reading in little endian systems and reading/writing in big endian systems and viceversa. Read how-to-write-endian-agnostic-c-c-code

    struct date *object=malloc(sizeof(struct date));
    strcpy(object->day,"Good day");
    object->month=6;
    object->year=2013;
    FILE * file= fopen("output", "wb");
    if (file != NULL) {
        fwrite(object, sizeof(struct date), 1, file);
        fclose(file);
    }
    

You can read them in the same way....using fread

    struct date *object2=malloc(sizeof(struct date));
    FILE * file= fopen("output", "rb");
    if (file != NULL) {
        fread(object2, sizeof(struct date), 1, file);
        fclose(file);
    }
    printf("%s/%d/%d\n",object2->day,object2->month,object2->year);
Community
  • 1
  • 1
pinkpanther
  • 4,770
  • 2
  • 38
  • 62
  • 5
    he should also be careful with structure padding at least – Giorgi Moniava Apr 02 '15 at 21:34
  • I have also written some C objects in the file and want to read them back and process them. But, i dont know which objects have been written into file, it could be any, so, in fread, i would not know sizeof() of the object i have been reading. Also, how to read exactly one objct at a time ? How fread could know that start and end of bytes in a file which represents object boundaries ? – Abhishek Sagar Mar 27 '17 at 03:55
  • 1
    I guess, if that's the case there should be a structure for the file, for example, let's say you write the type in one line and the object in another line etc.. then from the program based on the type you can know the size.. – pinkpanther Mar 27 '17 at 07:12
  • Why is in my case this script save only some `O�[name���)�����������.N=�'@�����������V)�mailH��)�� @���� @`@б���` ? Is this because the character encoding is wrong, or else? – gabor aron Oct 29 '18 at 18:35
  • @gaboraron Did you grab those by opening the file with text editor? – pinkpanther Oct 29 '18 at 18:48
  • @pinkpanther yes, I used simple `cat` and Joe, but both with the same result – gabor aron Oct 29 '18 at 18:55
  • And one more question: how do you read all structs from that file? So to the end of file, Because I tried many ways, but I always got `segmentation` error or other errors, And I didn't find this case. Everywhere only reading characters to the end, not `struct`s. – gabor aron Oct 30 '18 at 01:07
  • 3
    @gaboraron The file written isn't meant be read by text editors because it's a serialized form of the in objects. If you want to read it you need to write a C program to read it. You need to look for the end of file. You should look into the output of fread. Look here: https://msdn.microsoft.com/en-us/library/kt0etdcs.aspx. Generally if it's positive then something is read. – pinkpanther Oct 30 '18 at 18:27