1 / 23

Fread Fwrite & Fgets Fputs & Ftell Rewind

Fread Fwrite & Fgets Fputs & Ftell Rewind. By: Ady J. Audain Ariel Caballero Alex Manzano Norwing Rivas. Description of Fread. #include < stdio.h > size_t fread  (void * restrict ptr , size_t size, size_t nmemb , FILE * restrict stream)

Download Presentation

Fread Fwrite & Fgets Fputs & Ftell Rewind

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. Fread Fwrite&FgetsFputs&Ftell Rewind By: Ady J. Audain Ariel Caballero Alex Manzano Norwing Rivas

  2. Description of Fread • #include <stdio.h> size_tfread (void * restrict ptr, size_t size, size_tnmemb, FILE * restrict stream) • The function fread() reads num number of objects (where each object is size bytes) and places them into the array pointed to by buffer. The data comes from the given input stream. The return value of the function is the number of things read...use |feof()| or |ferror()| to figure out if an error occurs Return Value • The function fread does not distinguish between end-of-file and error, and callers must use feof and ferror to determine which occurred

  3. Description of Fwrite • #include <stdio.h>size_t fwrite (const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream) • The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the number of objects written. Return Value • The function fwrite returns a value less than nmemb only if a write error has occurred.

  4. Block Diagram of Fread Fwrite Initializes Start of program Initialize Array 1 Opens a file in binary mode Saves Array 1 to file Opens the file for reading in binary mode Read the data into Array 2 Display both arrays to show they’re the same

  5. Fread Fwrite Code #include <stdio.h> //includes the standard in out library. #include <stdlib.h> //includes the standard library. #define SIZE 20 //macro that defines SIZE to a length of 20. main() { int count, array1[SIZE], array2[SIZE]; //declares integer count, array1 and array 2 both of size 20. FILE *fp; // declares a file pointer "fp" /* Initialize array1[]. */ for (count = 0; count < SIZE; count++) array1[count] = 2 * count; //initializes array1 with elements from 0 through 19 by multiples of 2. so the elements are 0,2,4...38.

  6. Fread Fwrite Code /* Open a binary mode file. */ if ( (fp = fopen("direct.txt", "wb")) == NULL) //creates the binary file "direct.txt" for writing with the "wb" mode also tests to see if the file is able to be created in the case that the file is write protected. { fprintf(stderr, "Error opening file."); //prints out a standard error on the text terminal. exit(1); //exit(1) is used instead of exit(0) due to abnormal termination of the program. } /* Save array1[] to the file. */ if (fwrite(array1, sizeof(int), SIZE, fp) != SIZE) //writes to the file "fp" from array1 with size arguments of 20. returns a 0 when the written amount is equal to 20 characters. { fprintf(stderr, "Error writing to file."); //prints out a standard error on the text terminal. exit(1); //exit(1) is used instead of exit(0) due to abnormal termination of the program. } fclose(fp); //closes the file "fp".

  7. Fread Fwrite Code /* Now open the same file for reading in binary mode. */ if ( (fp = fopen("direct.txt", "rb")) == NULL) //opens the binary file "fp" as read "rb" and checks to see if the file can be opened in the case it doesn't exist. { fprintf(stderr, "Error opening file."); //prints out a standard error on the text terminal. exit(1); //exit(1) is used instead of exit(0) due to abnormal termination of the program. } /* Read the data into array2[]. */ if (fread(array2, sizeof(int), SIZE, fp) != SIZE) //reads the file "fp" and stores it into array2 with size arguments of 20. returns a 0 when the written amount is equal to 20 characters. { fprintf(stderr, "Error reading file."); //prints out a standard error on the text terminal. exit(1); //exit(1) is used instead of exit(0) due to abnormal termination of the program. } fclose(fp); //closes the file "fp".

  8. Fread Fwrite Code /* Now display both arrays to show they're the same. */ for (count = 0; count < SIZE; count++) printf("%d\t%d\n", array1[count], array2[count]); //prints out elements of arrays 1 and 2 from 0 through 19 in order of array1 followed by a tab /t followed by array2 }

  9. Program Output Example

  10. Example of Fread Fwrite #include <stdio.h> int main() { FILE *f; char buffer[10]; if (f = fopen("numbers2.txt", "rt")) { fread(buffer, 1, 10, f); buffer[10] = 0; fclose(f); printf("first 10 characters of the file:\n%s\n", buffer); } return 0; }

  11. Description of Ftell • #include <stdio.h> long int ftell(FILE *stream); • The ftell() function obtains the current value of the file-position indicator for the stream pointed to by stream.

  12. Description of Rewind • #include <stdio.h> void rewind( FILE *f ); • The function rewind() moves the file position indicator to the beginning of the specified stream, also clearing the error and EOF flags associated with that stream.

  13. Block Diagram of Ftell & rewind Code Demonstrates Ftell() and rewind(). Now open the file for reading. Read in 5 characters. Read in the next 5 characters. Rewind the stream. Read in 5 characters.

  14. Ftell & rewind Code Description   #include <stdio.h>   //includes the standard in out library. #include <stdlib.h>   //includes the standard library.#define BUFLEN 6       //macro that defines BUFLEN to a length of 6. char msg[] = "abcdefghijklmnopqrstuvwxyz";     //creates a character array string and initializes the alphabet in the array. main()     //initializes the program {     FILE *fp;   // declares a file pointer "fp"     char buf[BUFLEN]; //creates a character array buf with a lenght of 6.     if ( (fp = fopen("TEXT.TXT", "w")) == NULL)  //creates the file "TEXT.TXT" for writing with the "w" mode also tests to see if the file is able to be created in the case that the file is write protected.     {         fprintf(stderr, "Error opening file.");  //prints out a standard error on the text terminal.         exit(1);   //exit(1) is used instead of exit(0) due to abnormal termination of the program.     }     if (fputs(msg, fp) == EOF) //writes the character string "msg" into the file "fp" also check to see if the end of the file has been reached     {         fprintf(stderr, "Error writing to file."); //prints out a standard error on the text terminal.         exit(1);   //exit(1) is used instead of exit(0) due to abnormal termination of the program.     }     fclose(fp);  //closes the file "fp".

  15. Ftell & rewind Code Description /* Now open the file for reading. */ if ( (fp = fopen("TEXT.TXT", "r")) == NULL) //opens the file "fp" as read "r" and checks to see if the file can be opened in the case it doesn't exist.     {         fprintf(stderr, "Error opening file."); //prints out a standard error on the text terminal.         exit(1);   //exit(1) is used instead of exit(0) due to abnormal termination of the program.     }      printf("\nImmediately after opening, position = %ld", ftell(fp));  //ftell is used to return the current value of the position indicator of the stream, %ld is long integer value. /* Read in 5 characters. */     fgets(buf, BUFLEN, fp);  //reads characters from the stream and stores BUFLEN - 1, which is 5, to the character array "buf".     printf("\nAfter reading in %s, position = %ld", buf, ftell(fp)); //prints out buf which is a string and prints out the current value of the position indicator of the stream.   /* Read in the next 5 characters. */     fgets(buf, BUFLEN, fp);  //reads the next 5 charactes from the stream and stores them to "buf".     printf("\n\nThe next 5 characters are %s, and position now = %ld",             buf, ftell(fp));  //prints out buf again and prints out the new current value of the position indicator of the stream.

  16. Ftell & rewind Code Description /* Rewind the stream. */     rewind(fp); //repositions the file pointer to the beggining of the file "fp"     printf("\n\nAfter rewinding, the position is back at %ld",             ftell(fp)); //prints out the new current value of the position indicator of the stream.  /* Read in 5 characters. */     fgets(buf, BUFLEN, fp);  //reads the first 5 characters of the stream and stores them to "buf".     printf("\nand reading starts at the beginning again: %s", buf);  //prints out the "buf" string.     fclose(fp); //closes the file "fp" again. }

  17. Program Output Example

  18. Ftell & Rewind Code Example • We basically took the same code and modified the code to make the following program output.

  19. Description of Fgets • #include <stdio.h>char fgets (char * restrict str, int size, FILE * restrict stream) • The fgets function reads at most one less than the number of characters specified by size from the given stream and stores them in the string str. Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained. If any characters are read and there is no error, a '\0' character is appended to end the string. • will read an entire line of text (max chars = n) into buffer until the newline character or n=max, whichever occurs first. The function places a NULL character after the last character in the buffer. The function will be equal to a NULL if no more data exists.

  20. Description of Fputs • #include <stdio.h>int fputs (const char *str, FILE *stream) • writes the characters in buffer until a NULL is found. The NULL character is not written to the output_file. • The function fputs writes the string pointed to by str to the stream pointed to by stream.

  21. Example of Fgets #include <stdio.h> int main() { FILE *out = fopen("numbers2.txt", "wt"); // open the text file "fred.txt" for writing fprintf(out, "I Love StarWars!\n"); // write some text to the file fclose(out); // close the stream, so all changes to the file are saved FILE *in = fopen("numbers2.txt", "rt"); // open the file "fred.txt" for reading char buffer[100]; // read the first line from the file fgets(buffer, 20, in); printf("first line of \"numbers2.txt\": %s\n", buffer); // display what we've just read fclose(in); return 0; // close the stream }

  22. Example of Fputs #include <stdio.h> int main() { // open text file for writing FILE *f = fopen("numbers2.txt", "wt"); if (f) { printf ("Something has been writen to \ numbres2.txt \ file \n"); fputs(" StarWars is cool !! ", f); fclose(f); } return 0; }

  23. Summary of Lessons Learned • Researching was the hardest part of file I/O • In order to use these codes a previous document has to be created, if not you will always get an error stating that the program could not open the file. • Ftell and rewind are similar to fgets and fputs

More Related