From the course: Master C Language Pointers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Saving and reading a record

Solution: Saving and reading a record - C Tutorial

From the course: Master C Language Pointers

Start my 1-month free trial

Solution: Saving and reading a record

(upbeat music) - [Danny] The reason for having two solution files for this exercise is so that you can see the data is both written and read. First, the writing. The key points are the structure definition at line six. It lacks any pointer members. That's good because you can't really save data to a file when the structure has a pointer member. Line 15 allocates the structure. The sizeof operator works on the structure definition, not the towrite variable. The structure is filled starting at line 23. The arguments to the fwrite function at line 45 are important. Again, the sizeof operator works on the structure definition, not the variable towrite. So far, so good. The same rule holds for the reading program shown here. The structure definition must be identical. Well, the member names could change but the member data types must be the same. Structure is allocated at line 14. On line 23, the file is opened. The…

Contents