1 / 25

Group 7

Group 7. Jimmy Mauri Hansel Lezcano Pablo Alvear Juana Ramirez Juan Gonzalez-Llanos. End of File ( EoF ) and File Seek ( Fseek ()). End of File ( EoF ). What is End of File? Programming parameter that defines the end of a file.

rossa
Download Presentation

Group 7

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 7 Jimmy Mauri Hansel Lezcano Pablo Alvear Juana Ramirez Juan Gonzalez-Llanos End of File (EoF) and File Seek (Fseek())

  2. End of File (EoF) What is End of File? • Programming parameter that defines the end of a file. • Once a source file is selected, when the file reaches its NULL value, EoF condition is met. • Ex. Reading a text file. Once the program has no more lines to read from, EoF is met.

  3. Program Objectives • Variables are defined • Program User Interface requests file • Once appropriate file is opened, read the file as a string text and print the lines until the end of file is met. • If inappropriate file is requested (or none), give error message and terminate. • Program terminates.

  4. Program Flowchart Creates variables Develop User Interface Open text file Read and display until End of File

  5. EoF Code #include <stdio.h> #include <stdlib.h> #define BUFSIZE 100 main() { char buf[BUFSIZE]; char filename[20]; FILE *fp; puts("Enter name of text file to display: "); gets(filename); if ( (fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } while ( !feof(fp) ) { fgets(buf, BUFSIZE, fp); printf("%s",buf); } fclose(fp); } Prints a line of instruction to user. Requests input from user.

  6. EoF Code #include <stdio.h> #include <stdlib.h> #define BUFSIZE 100 main() { char buf[BUFSIZE]; char filename[20]; FILE *fp; puts("Enter name of text file to display: "); gets(filename); if ( (fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } while ( !feof(fp) ) { fgets(buf, BUFSIZE, fp); printf("%s",buf); } fclose(fp); } Prints a line of instruction to user. Requests input from user. Opens the file. If the file is not found (null), then error message is printed.

  7. EoF Code #include <stdio.h> #include <stdlib.h> #define BUFSIZE 100 main() { char buf[BUFSIZE]; char filename[20]; FILE *fp; puts("Enter name of text file to display: "); gets(filename); if ( (fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } while ( !feof(fp) ) { fgets(buf, BUFSIZE, fp); printf("%s",buf); } fclose(fp); } Prints a line of instruction to user. Requests input from user. Opens the file. If the file is not found (null), then error message is printed. Program will continue to print as long as end of file is not met.

  8. EoF Code #include <stdio.h> #include <stdlib.h> #define BUFSIZE 100 main() { char buf[BUFSIZE]; char filename[20]; FILE *fp; puts("Enter name of text file to display: "); gets(filename); if ( (fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } while ( !feof(fp) ) { fgets(buf, BUFSIZE, fp); printf("%s",buf); } fclose(fp); } Prints a line of instruction to user. Requests input from user. Opens the file. If the file is not found (null), then error message is printed. Program will continue to print as long as end of file is not met. File closed and all changes saved.

  9. File Seek Fseek() What is Fseek()? • Program command to search for a specific spot in a file. • Once found, additional code can be written to modify the file on the position where Fseek() was instructed to locate. • Example: Modifying a text file using program code to search for a position in the text and rewrite it.

  10. Parameters of Fseek() stream • Pointer to a FILE object that identifies the stream offset • Number of bytes to offset from origin. origin • Position from where offset is added. It is specified by one of the following constants defined in <cstdio>: SEEK_SET Beginning of file SEEK_CUR Current position of the file pointer SEEK_END End of file

  11. Program Flowchart Creates file Writes to file Reads the file Seeks file element Print element

  12. Fseek() Code Example #include <stdio.h> #include <stdlib.h> #include <io.h> #define MAX 50 main() { FILE *fp; int data, count, array[MAX]; long offset; for (count = 0; count < MAX; count++) array[count] = count * 10; if ( (fp = fopen("RANDOM.DAT", "wb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } if ( (fwrite(array, sizeof(int), MAX, fp)) != MAX) { fprintf(stderr, "\nError writing data to file."); exit(1); } fclose(fp); Initializes the Array fom (0x10) to (49x10)

  13. Fseek() Code Example #include <stdio.h> #include <stdlib.h> #include <io.h> #define MAX 50 main() { FILE *fp; int data, count, array[MAX]; long offset; for (count = 0; count < MAX; count++) array[count] = count * 10; if ( (fp = fopen("RANDOM.DAT", "wb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } if ( (fwrite(array, sizeof(int), MAX, fp)) != MAX) { fprintf(stderr, "\nError writing data to file."); exit(1); } fclose(fp); Initializes the Array fom (0x10) to (49x10) fopen() will open a binary file to modify. If one doesn’t exist, it will create it. Should the “if” statement hit a null value, an error will be printed.

  14. Fseek() Code Example #include <stdio.h> #include <stdlib.h> #include <io.h> #define MAX 50 main() { FILE *fp; int data, count, array[MAX]; long offset; for (count = 0; count < MAX; count++) array[count] = count * 10; if ( (fp = fopen("RANDOM.DAT", "wb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } if ( (fwrite(array, sizeof(int), MAX, fp)) != MAX) { fprintf(stderr, "\nError writing data to file."); exit(1); } fclose(fp); Initializes the Array fom (0x10) to (49x10) fopen() will open a binary file to modify. If one doesn’t exist, it will create it. Should the “if” statement hit a null value, an error will be printed. Writes the array to the file. A maximum of 50 objects will be written defined by MAX.

  15. Fseek() Code Example #include <stdio.h> #include <stdlib.h> #include <io.h> #define MAX 50 main() { FILE *fp; int data, count, array[MAX]; long offset; for (count = 0; count < MAX; count++) array[count] = count * 10; if ( (fp = fopen("RANDOM.DAT", "wb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } if ( (fwrite(array, sizeof(int), MAX, fp)) != MAX) { fprintf(stderr, "\nError writing data to file."); exit(1); } fclose(fp); Initializes the Array fom (0x10) to (49x10) fopen() will open a binary file to modify. If one doesn’t exist, it will create it. Should the “if” statement hit a null value, an error will be printed. Writes the array to the file. A maximum of 50 objects will be written defined by MAX. Closes the file and saves all edits to the file memory..

  16. Fseek() Code Continued if ( (fp = fopen("RANDOM.DAT", "rb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } while (1) { printf("\nEnter element to read, 0-%d, -1 to quit: ",MAX-1); scanf("%ld", &offset); if (offset < 0) break; else if (offset > MAX-1) continue; if ( (fseek(fp, (offset*sizeof(int)), SEEK_SET)) != NULL) { fprintf(stderr, "\nError using fseek()."); exit(1); } fread(&data, sizeof(int), 1, fp); printf("\nElement %ld has value %d.", offset, data); } fclose(fp); } Fopen() will open the file. If the statement acquires a null value, the program will print an error message for the user.

  17. Fseek() Code Continued if ( (fp = fopen("RANDOM.DAT", "rb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } while (1) { printf("\nEnter element to read, 0-%d, -1 to quit: ",MAX-1); scanf("%ld", &offset); if (offset < 0) break; else if (offset > MAX-1) continue; if ( (fseek(fp, (offset*sizeof(int)), SEEK_SET)) != NULL) { fprintf(stderr, "\nError using fseek()."); exit(1); } fread(&data, sizeof(int), 1, fp); printf("\nElement %ld has value %d.", offset, data); } fclose(fp); } Fopen() will open the file. If the statement acquires a null value, the program will print an error message for the user. While function allows the system to continue displaying elements until the user inputs -1 to terminate program.

  18. Fseek() Code Continued if ( (fp = fopen("RANDOM.DAT", "rb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } while (1) { printf("\nEnter element to read, 0-%d, -1 to quit: ",MAX-1); scanf("%ld", &offset); if (offset < 0) break; else if (offset > MAX-1) continue; if ( (fseek(fp, (offset*sizeof(int)), SEEK_SET)) != NULL) { fprintf(stderr, "\nError using fseek()."); exit(1); } fread(&data, sizeof(int), 1, fp); printf("\nElement %ld has value %d.", offset, data); } fclose(fp); } Fopen() will open the file. If the statement acquires a null value, the program will print an error message for the user. While function allows the system to continue displaying elements until the user inputs -1 to terminate program. Function will move the position indicator to the reference point (start of the file pointer in this case). An error message is printed fseek doesn’t return a zero value.

  19. Fseek() Code Continued if ( (fp = fopen("RANDOM.DAT", "rb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } while (1) { printf("\nEnter element to read, 0-%d, -1 to quit: ",MAX-1); scanf("%ld", &offset); if (offset < 0) break; else if (offset > MAX-1) continue; if ( (fseek(fp, (offset*sizeof(int)), SEEK_SET)) != NULL) { fprintf(stderr, "\nError using fseek()."); exit(1); } fread(&data, sizeof(int), 1, fp); printf("\nElement %ld has value %d.", offset, data); } fclose(fp); } Fopen() will open the file. If the statement acquires a null value, the program will print an error message for the user. While function allows the system to continue displaying elements until the user inputs -1 to terminate program. Function will move the position indicator to the reference point (start of the file pointer in this case). An error message is printed fseek doesn’t return a zero value. Reads a single integer, stores it in memory and prints it.

  20. Easy Example Program Objectives • Variables are defined • Program User Interface opens a text tile. • Once appropriate file is opened, Fseek() is used as a pointer to reference starting edit point. • Once location is acquired, a replacement string will edit the text file beginning at given point. • Program terminates.

  21. Fseek() Code Easy Example #include <stdio.h> #include <stdlib.h> int main () { FILE * pFile; pFile = fopen ( "example.txt" , "w" ); fputs ( "This is an apple." , pFile ); fseek ( pFile , 12 , SEEK_SET ); fputs ( "wful presentation!" , pFile ); fclose ( pFile ); return 0; }

  22. BEFORE

  23. AFTER

More Related