470 likes | 611 Vues
Group 6 Marco Brilhante Rene Santiago Wissam Tamim. Fread Fwrite Fgets Fputs. Fread and Fwrite. 1. Outline parts of program 2. Block Diagram 3. Highlighted parts of code 4. Describe Code in detail 5. Program output example 6. Similar Example. Why do we need these functions?.
E N D
Fread and Fwrite 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Describe Code in detail5. Program output example6. Similar Example
Why do we need these functions? • We need these functions because they are the only means by which we can actually ‘save’ and retrieve data into secondary storage devices. • In other words, saving data in variables or arrays is not possible because this data is temporarily ‘stored’ in random access memory. • Files, however, enable us to save and retrieve data.
What are file manipulation functions? • Fwrite, fread, fgets, fputs are functions which are provided by the standard library. • These functions are language specific. • Each of these functions has a stream or ‘bridge’ associated with it that enables it to work properly.
1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
Fread and Fwrite • Outline of the program • -Create two arrays, “array1” & “array2”. • -Fill up the “array1” with a sequence of numbers. • -Use “fopen” to create a text file. • Use “Fwrite” to save the contents of “array1” into the text file created. • -Use “fread” to save the contents of the file, into “array2” • -Print the contents of “array2” to verify the data is the same.
1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
Legend of Highlighted and Non-highlighted Portions Blue Portion(s): ‘For’statements Yellow Portion(s): ‘If’ statements of binary write mode Green Portion(s): ‘If’ statements of binary read mode Red Font(s): Comments Non-highlighted Portion(s): Other program parts (e.g., declaration of variables, preprocessor directives, etc.)
How do they work? Fwrite(array1, sizeof(int), SIZE, fp) File Fread(array2, sizeof(int), SIZE, fp) File
1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
From Preprocessor Directives to Initilization of First Array Preprocessor Directives Program Execution and Beginning Declarations Initialization of First Array
The Binary Write Mode Opening Binary Mode File for Writing Saving the First Array to the File Closing the Binary Write Mode Stream
The Binary Read Mode and the Program Output Opening Binary Mode File for Reading Reading Data into Second Array Closing the Binary Read Mode Stream Displaying Both Arrays
1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
Program Images Program Code Program Output
1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Describe Code in detail5. Program output example6. Similar Example
Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
What Does The Program Do ? • Creates a file, and puts a string of characters in it. • Reads the string of letters from the file until designated point. • If asked again, reads the next following letters until designated point. • Then it returns to beginning of string.
Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
Legend of Highlighted and Non-highlighted Portions Yellow Portion(s): “fopen” commands embedded in the “if” statement for opening the text file Green Portion(s): ‘fputs’ for adding the string to the text file Blue Portion(s): ‘Ftell’statements Red Font(s): Comments Red highlights (s): the rewind command Non-highlighted Portion(s): Other program parts (e.g., declaration of variables, preprocessor directives, printf commands etc.)
Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
How do they work? Fgets(buf, BUFLEN, fp) File Fputs(buf,fp) File
Fgets / Fputs • <stdio.h> Uses “streams” to operate with physical devices & computer terminals • <stdlib.h> Defines several functions: dynamic memory management, random number generation, communication with the environment, etc. • Define BUFLEN 6 • Char msg[]=“abcdefghijklmnopqrstuvwxyz”
Fgets / Fputs • Main(){ • FILE *fp: A pointer to a FILE object uniquely identifies a stream, and is used as a parameter in the operations involving that stream. • Create a buf[6] array, which will be used to store chunks of the stream • Fopen: included in “<stdio.h>” , ret = fopen( name, options ) • Returns [a pointer to a FILE object that is used to identify the stream] / NULL • "file pointer". This points to a structure that contains information needed for subsequent I/O on the file being opened • If function: serves 2 purposes.
Fgets / Fputs • Fputs(msg,fp): writes the string pointed to by msg to the stream pointed to by fp • returns 0 on success and EOF on error • If statement serves 2 purposes
Fgets / Fputs • Using the same concept as before, open “TEXT.TXT” in read mode • Check for errors • “stderr “ used as an argument for any function that expects an output stream
Fgets / Fputs • Ftell(fp): Returns the current value of the position indicator of the stream. • Output set to long decimal
Fgets / Fputs • char fgets (char * restrict str, int size, FILE * restrict stream) • Reads at most (BUFLEN-1) • Source fp • Destination buf • Perform “ftell(fp)” and get the current position of the stream
Fgets / Fputs • Perform “fgets()” again • print what was retrieved • the and display new position
Fgets / Fputs • Rewind(): included in <stdio.h> library • adjusts the specified file so that the next I/O operation will take place at the beginning of the file. • Do “ftel(fp)”, and verify position is back at zero
Fgets / Fputs • Do “fgets()” again, display the output and verify it’s the beginning of the file
Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
Program Images Program Code Program Code
Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example
Sample Program Code Sample Program Output
Lessons Learned • How to Manipulate files. • How to use Fwrite / Fread and Fgets / Fputs to do so.
QUIZ! • How many arguments does “Fwrite” take? • Ans: 4 • How many arguments does “Fread” take? • Ans:4 • What does “Fputs” do? • Ans: send/copies a string into a file. • What does “Fgets” stand for? • Ans: file get string • What did “Ftell” do? • Ans: returns current value of the position indicator of the stream • What does “fopen” return if there’s an error? • Ans: NULL
References • http://www.cprogramming.com/tutorial/cfileio.html • http://www.cplusplus.com/reference/clibrary/cstdio/ftell/ • http://www.codecogs.com/reference/c/stdio.h/fputs.phphttp://www.codecogs.com/reference/c/stdio.h/fgets.php • http://www.thinkage.ca/english/gcos/expl/c/lib/rewind.html • http://cboard.cprogramming.com/c-programming/4996-fputs-sequential-file.html