140 likes | 289 Views
Group 6 fread , fwrite / fgets , fputs. William Martinez Benjamin Allen Javier Morejon Derek Gutierrez Sebastian Gonzalez. What are they?. fwrite - The fwrite function is used to write data to a file fread - The fread function is used to read data from a file
E N D
Group 6fread, fwrite/ fgets, fputs William Martinez Benjamin Allen Javier Morejon Derek Gutierrez Sebastian Gonzalez
What are they? • fwrite- The fwrite function is used to write data to a file • fread- The fread function is used to read data from a file • fgets-Reads characters from stream and stores them as a C string. • fputs-put the string on a stream. • ftell-Returns the current position of the file read/write pointer. • rewind- Takes the pointer into a file to the beginning of the stream.
fread/fwrite Include libraries. Declare arrays. Declares int and FILE pointer. Establishes values of the elements of the array. Opens the FILE and retrieves the data and places it into another array. Opens the FILE and writes the contents of the array into the file. Displays both arrays in order to show that they are both the same.
fgets/fputs Include necessary libraries. Define the length of the character reading buffer. Initializes the character array. Reads the first 5 characters from the FILE and gives the position indicator. Creates a file and writes the entire contents of the array into the FILE. Reads the first 5 characters again and gives the position indicator again, demonstrating that the stream was rewound. Reads the next 5 characters from the FILE and gives the position indicator. Relocates the position indicator to the beginning of the FILE.
Alternate Examples Two screen captures of alternate examples, showing the use of fread/fwrite and fputs/fgets, respectively.
You can read or write data to a file as a whole or piece by piece, depending on which function you use. The position indicator never moves on its own. It must be moved by reading characters or a separate command (stays where it was after the last statement in which it was used). Using these four functions requires a knowledge of the functions fopen and fclose. Lessons Learned