1 / 27

 Group 6 : Fread , Fwrite , Fgets , Fputs

 Group 6 : Fread , Fwrite , Fgets , Fputs. By: Andy Avalos, Khampetch Seignarack , Osman Delgado, Tarif Khan. Problem solving:. What are: Fread , Fwrite , Fgets , Fputs ? How are they used in a program?. Import Libraries fwrite fread. Import Libraries fget fput. Fwrite Fread.

gabi
Download Presentation

 Group 6 : Fread , Fwrite , Fgets , Fputs

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. EEL 2880 - Fall 2010 U02  Group 6: Fread, Fwrite, Fgets, Fputs By: Andy Avalos, KhampetchSeignarack, Osman Delgado, Tarif Khan.

  2. Problem solving: • What are: Fread, Fwrite, Fgets, Fputs? • How are they used in a program? EEL 2880 - Fall 2010 U02

  3. Import Libraries fwritefread EEL 2880 - Fall 2010 U02

  4. Import Libraries fgetfput EEL 2880 - Fall 2010 U02

  5. FwriteFread • We will now try to explore this program through each line. • Follow us on this exciting journey… here we go! • This section by: Andy Avalos, Osman Delgado. EEL 2880 - Fall 2010 U02

  6. Fread • Read block of data from stream • Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr.The position indicator of the stream is advanced by the total amount of bytes read.The total amount of bytes read if successful is (size * count).Parameters • ptr Pointer to a block of memory with a minimum size of (size*count) bytes. size Size in bytes of each element to be read. count Number of elements, each one with a size of size bytes. stream Pointer to a FILE object that specifies an input stream. Return Value • The total number of elements successfully read is returned as a size_t object, which is an integral data type.If this number differs from the count parameter, either an error occured or the End Of File was reached.You can use either ferror or feof to check whether an error happened or the End-of-File was reached. EEL 2880 - Fall 2010 U02

  7. Fwrite • Write block of data to stream • Writes an array of count elements, each one with a size of size bytes, from the block of memory pointed by ptr to the current position in the stream.The position indicator of the stream is advanced by the total number of bytes written.The total amount of bytes written is (size * count). • Parameters • ptr Pointer to the array of elements to be written. size Size in bytes of each element to be written. count Number of elements, each one with a size of size bytes. stream Pointer to a FILE object that specifies an output stream. • Return Value • The total number of elements successfully written is returned as a size_t object, which is an integral data type.If this number differs from the count parameter, it indicates an error. EEL 2880 - Fall 2010 U02

  8. Refer to Lines 1-8 • Line 1: stdio.h stands for standard input output header. • Line 2: stdlib stands for "standard library". • Line 3: Stands for the size of the array after it is run. • Line 4: Begins the program. • Lines 5,6: Count, array1, and array 2 are set to be integers. Array1 and Array 2 are set to 20 by SIZE. Declares array1 and array2 that are later going to be used, and uses a pointer to the FILE. FILE identifies a stream and contains info needed to control it. • Lines 7,8: For loop that starts the count of array 1 up to one less than the size, calls the array and declares the pattern of increments by count * 2. EEL 2880 - Fall 2010 U02

  9. Refer to Lines 9-14 • Line 9: Opens up the file named “direct.txt”. “wb” is the parameters, w creates the empty file for ‘writing’ and b means that it’s written in binary. ‘NULL’ is equal to the created empty file. • Line 10,11: Does not let the loop keep repeating if there’s an error while opening the file. (EX: It does not exist) “stderr” stands for standard error stream and is the default destination for error messages warnings. • Line 12: It will copy the entire array 1 in to the file “direct.txt.” • Line 13, 14: Prevents the repetition of the loop if there’s an error while saving the array 1. EEL 2880 - Fall 2010 U02

  10. Refer to Lines 15-24 • Line 15: Closes array 1. • Line 16: Opens the file named “direct.txt” for use with array 2, if a written array1 does not have the size of the fp. • Lines 17,18: Prevents the loop if there’s an error while opening “direct.txt” • Line 19: Reads the size from array 1 into array 2. • Lines 20, 21: Stops the error if something goes wrong while the data is being read into array2. • Line 22: Closes (in this case array 2) the file associated with the stream and disassociates it. • Lines 23, 24: These lines will print the values of array1 & array2 with the value of count and show how array 1 and 2 are the same after the program is compiled and run. EEL 2880 - Fall 2010 U02

  11. FreadFwriteBlockdiagram Libraries Global definitions fclose(fp) If fread doesn’t equal 20, prints “error” and exits. Main Sets integers: count, array1, array2 fclose(fp) If all conditions are met: For loop from count to 20 and it fprints both array1 and array2 Point to FILE (File*fp) For loop from int count to 20 ,sets array numbers to be multiples of 2 • If fopen is NULL, prints “error” and exits If fwrite doesn’t equal 20, prints “error” and exits EEL 2880 - Fall 2010 U02

  12. FreadFwrite Output EEL 2880 - Fall 2010 U02

  13. Fwrite Extra Example • This example shows how Fwrite would copy the given char str’s • Open a text file ‘mytext’ • Write in the string of 49 chars into the ‘text’ file Then close. • If you went to the folder where the program was ran to, you would have a new text file with the words you placed in between char str[] = “ H E R E ” ; EEL 2880 - Fall 2010 U02

  14. Fwrite Extra Example Output EEL 2880 - Fall 2010 U02

  15. Now we move onto FgetsFputs • This section by: KhampetchSeignarack, Tarif Khan. • Less exciting than the last part… EEL 2880 - Fall 2010 U02

  16. Fgets • Get string from stream • Reads characters from stream and stores them as a C string into str until a set number characters have been read or either a newline or an End-of-File is reached, whichever comes first.A newline character makes fgets stop reading and a null character is automatically added in str after the characters are read, to indicate the end of the C string. EEL 2880 - Fall 2010 U02

  17. Fgets • Get string from stream • Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first.A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str.A null character is automatically appended in str after the characters read to signal the end of the C string. • Parameters • str Pointer to an array of chars where the string read is stored. num Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used. stream Pointer to a file object that identifies the stream where characters are read from.To read from the standard input, stdin can be used for this parameter. • Return Value • On success, the function returns the same str parameter.If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned.If an error occurs, a null pointer is returned.Use either ferror or feof to check whether an error happened or the End-of-File was reached. EEL 2880 - Fall 2010 U02

  18. Fputs • Write string to stream • Fputs writes the string to stream pointed by (str).The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). The final null-character is not written to the stream. EEL 2880 - Fall 2010 U02

  19. Fputs • Write string to stream • Writes the string pointed by str to the stream.The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to the stream. • Parameters • str An array containing the null-terminated sequence of characters to be written. stream Pointer to a FILE object that identifies the stream where the string is to be written. • Return Value • On success, a non-negative value is returned.On error, the function returns EOF. EEL 2880 - Fall 2010 U02

  20. Refer to lines 1-14 Line 3: standard input output header. Line 4: studio library header. Line 6: is setting BUFLEN of 6 integers to be place in local variable area for later use in the program. Line 8: is declaring a character array. Line 10: is the beginning of program. Line 12,13: is creating a pointer array to File. EEL 2880 - Fall 2010 U02

  21. Refer to lines 15-30 Line 15: opens the file named “text.txt” for reading. Line 17,18: will print “Error opening file” and stops loop. If an error occurs while opening file. Line 21: will write End-of-file into the file Line 23,24: will print “Error writing to file” and stops loop. If an error occurs while writing to file. Line 27: closes the array. EEL 2880 - Fall 2010 U02

  22. Refer to lines 31-41 Line 31: opens file named “text.txt” for read. Line 33,34: prints “Error opening file” and stops the loop. If an error occurs while opening file. Line 36: prints newline and sets first position to zero. Line 40,41: reads the first five characters then prints a newline and set position to five EEL 2880 - Fall 2010 U02

  23. Refer to lines 43-60 Line45,46: reads the next five characters then prints the newline and set position to ten. Line 51: starts the reading back to position zero. Line 53: prints newline after once it starts back at position zero. Line 57,58: reads the next five characters again. Line 60: closes the array. EEL 2880 - Fall 2010 U02

  24. FgetsFputs Output EEL 2880 - Fall 2010 U02

  25. FgetsFputs Block diagram If fopen=NULL Frprint Error Message Libraries Global definitions “char msg array “defined exit Main If all conditions satisfied: Printf (after opening file) FILE *fp Fgets and prints after reading in position “Char buf array” defined Fgets prints “the next 5 char” and new position If fopen is = NULL Fprintf Error Message Rewind prints where the position is back at Fgets and reads at beginning. exit • If fputs = EOF • FprintfErrorMessage fclose exit fclose EEL 2880 - Fall 2010 U02

  26. References http://www.cplusplus.com/reference/clibrary/cstdio/fread/ http://www.cplusplus.com/reference/clibrary/cstdio/fputs/ http://www.cplusplus.com/reference/clibrary/cstdio/fgets/ http://www.codecogs.com/reference/c/stdio.h/fread.php • C How to program (sixth edition) by Paul Deitel & Harvey Deitel: (Chapter 10, and pretty much every other chapter.) EEL 2880 - Fall 2010 U02

  27. Summary of lessons learned: • ‘wb’ stands for w is write and b is binary. • “stderr” stands for standard error stream and is the default destination for error messages. • Rewind is a library function that sends the position back to the beginning EEL 2880 - Fall 2010 U02

More Related