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.

Saving pointers and buffers

Saving pointers and buffers - C Tutorial

From the course: Master C Language Pointers

Start my 1-month free trial

Saving pointers and buffers

- [Instructor] When saving a buffer to a file, ensure that you save the data and not the pointer. To illustrate consider this code. Storage for 10 integers is allocated and then filled. A file is opened at line 25, and key to the operation is the fwrite function at line 33. The arguments are the address of the data chunk. Pointer base in this case. The size of each item to write integer and then the quantity, 10 integers and the file pointer. Build and run. And 10 integers are written to the file integers.dat. The output is in informative, but did it work? This code reads the data file using a similar technique as shown in the first exercise. So a buffer is allocated here in line 10, a file is open for reading here at line 18, and at line 26 the fread function reads in the 10 integers. Integers or output in the for loop down here at line 39. Build and run, and it all works, thank you. Where this operation can go…

Contents