1 / 47

Group 6 Marco Brilhante Rene Santiago Wissam Tamim

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?.

vail
Télécharger la présentation

Group 6 Marco Brilhante Rene Santiago Wissam Tamim

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Group 6 Marco Brilhante Rene Santiago Wissam Tamim

  2. Fread FwriteFgets Fputs

  3. Fread and Fwrite 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Describe Code in detail5. Program output example6. Similar Example

  4. 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.

  5. 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.

  6. 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  7. 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.

  8. 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  9. Fread and FwriteBlock Diagram

  10. 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  11. 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.)

  12. How do they work? Fwrite(array1, sizeof(int), SIZE, fp) File Fread(array2, sizeof(int), SIZE, fp) File

  13. 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  14. From Preprocessor Directives to Initilization of First Array Preprocessor Directives Program Execution and Beginning Declarations Initialization of First Array

  15. The Binary Write Mode Opening Binary Mode File for Writing Saving the First Array to the File Closing the Binary Write Mode Stream

  16. 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

  17. 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  18. Program Images Program Code Program Output

  19. 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  20. FreadFwriteExample

  21. FreadFwrite Example Output

  22. Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Describe Code in detail5. Program output example6. Similar Example

  23. Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  24. 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.

  25. Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  26. Fgets / Fputs

  27. Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  28. 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.)

  29. Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  30. How do they work? Fgets(buf, BUFLEN, fp) File Fputs(buf,fp) File

  31. 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”

  32. 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.

  33. 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

  34. 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

  35. Fgets / Fputs • Ftell(fp): Returns the current value of the position indicator of the stream. • Output set to long decimal

  36. 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

  37. Fgets / Fputs • Perform “fgets()” again • print what was retrieved • the and display new position

  38. 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

  39. Fgets / Fputs • Do “fgets()” again, display the output and verify it’s the beginning of the file

  40. Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  41. Program Images Program Code Program Code

  42. Fgets / Fputs 1. Outline parts of program2. Block Diagram 3. Highlighted parts of code4. Code in detail5. Program output example6. Similar Example

  43. Sample Program Code Sample Program Output

  44. Lessons Learned • How to Manipulate files. • How to use Fwrite / Fread and Fgets / Fputs to do so.

  45. 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

  46. 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

  47. Questions

More Related