1 / 13

Group 2 Presentation Fread & Fwrite , Fgets & Fputs

Hector Trujillo Javier Oramas Sergio Luque Christopher Jimenez Michael Alan Abraham Osman. Group 2 Presentation Fread & Fwrite , Fgets & Fputs. Main Project Functions. Sequential-Access file functions, used to record data: Fgets : Read a line from a stream.

olympe
Download Presentation

Group 2 Presentation 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. Hector Trujillo Javier Oramas Sergio Luque Christopher Jimenez Michael Alan Abraham Osman Group 2 PresentationFread& Fwrite, Fgets & Fputs

  2. Main Project Functions Sequential-Access file functions, used to record data: • Fgets: Read a line from a stream. • Fputs: Write a line to a stream. Random-Access file functions, used to quickly search through records: • Fwrite: Creates stream. • Fread: Gathers data from stream.

  3. Standard Library Functions • Stdio.h (Standard input/output library): Contains function prototypes for the standard input/output library functions, and information used by them. • Stdlib.h (General utilities library): Contains function prototypes for conversions of numbers to text and text to numbers, memory allocation, random numbers, and other utility functions.

  4. Fgets & Fputs This program creates a file with the character string “abcdefghijklmnopqrstuvxyz” and a prompt window that prints characters using their positions

  5. Flow Chart

  6. Bubble Diagram Tries to find file and if it doesn’t exist it create File TEXT.txt Saves array msg into file TEXT.txt Closes TEXT.txt Prints buf and the position indicator Reads the first 5 characters and stores into array buf and increments position of stream indicator Opens file TEXT.txt Reads the next 5 characters and stores into array buf and increments position of stream indicator Rewinds stream indicator Prints stream indicator position and array buf Closes TEXT.txt

  7. Explanation • Declare pointer fp • Declare Character Array • Creates a text file called TEXT and checks if file is created. • Attempts to write array msg and checks if file is written. • File is closed. • Opens file in read mode • Prints text plus initial position of stream indicator, then prints first five characters in array, then prints next five characters in the array. • Stream indicator rewinds • Prints new value for stream indicator • Prints buf /* Demonstrates ftell() and rewind(). */ #include <stdio.h> #include <stdlib.h> #define BUFLEN 6 char msg[] = "abcdefghijklmnopqrstuvwxyz"; main() { FILE *fp; char buf[BUFLEN]; if ( (fp = fopen("TEXT.TXT", "w")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } if (fputs(msg, fp) == EOF) { fprintf(stderr, "Error writing to file."); exit(1); } fclose(fp); /* Now open the file for reading. */ if ( (fp = fopen("TEXT.TXT", "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } printf("\nImmediately after opening, position = %ld", ftell(fp)); /* Read in 5 characters. */ fgets(buf, BUFLEN, fp); printf("\nAfter reading in %s, position = %ld", buf, ftell(fp)); /* Read in the next 5 characters. */ fgets(buf, BUFLEN, fp); printf("\n\nThe next 5 characters are %s, and position now = %ld", buf, ftell(fp)); /* Rewind the stream. */ rewind(fp); printf("\n\nAfter rewinding, the position is back at %ld", ftell(fp)); /* Read in 5 characters. */ fgets(buf, BUFLEN, fp); printf("\nand reading starts at the beginning again: %s", buf); fclose(fp); }

  8. Fread & Fwrite This program creates a file and array1 is saved and a prompt window that shows that both array1 and array2 are identical

  9. Flow Chart

  10. Bubble Diagram Creates count and two arrays as well as File pointer Initializes array1 (20 integers) to twice the value of count Creates/opens direct.txt in binary mode or prints error Opens file in binary read mode or prints error Saves array1 to file or prints error Closes direct.txt Reads contents of File into array 2 or prints error. Closes file and prints both arrays to show they are the same.

  11. Explanation • Count, array1, array2 and file pointer are declared. • For loop is used to initialize array1 and it is incremented to a value twice of count. • File direct.txt is either created or opened, otherwise an error message will be prompted. • Array1 is saved in direct.txt. • File is closed. • File is opened for reading in binary mode, otherwise an error message will be prompted. • Reads contents of file into array2, otherwise an error message will be prompted. • File is closed • A for loop is used to print the contents of both arrays and show that they are same. /* Direct file I/O with fwrite() and fread(). */ #include <stdio.h> #include <stdlib.h> #define SIZE 20 main() { int count, array1[SIZE], array2[SIZE]; FILE *fp; /* Initialize array1[]. */ for (count = 0; count < SIZE; count++) array1[count] = 2 * count; /* Open a binary mode file. */ if ( (fp = fopen("direct.txt", "wb")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } /* Save array1[] to the file. */ if (fwrite(array1, sizeof(int), SIZE, fp) != SIZE) { fprintf(stderr, "Error writing to file."); exit(1); } fclose(fp); /* Now open the same file for reading in binary mode. */ if ( (fp = fopen("direct.txt", "rb")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } /* Read the data into array2[]. */ if (fread(array2, sizeof(int), SIZE, fp) != SIZE) { fprintf(stderr, "Error reading file."); exit(1); } fclose(fp); /* 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]) }

  12. Example Fgets

  13. Example Fwrite

More Related