260 likes | 494 Views
EOF and Fseek in C Programming Group #7. Members Daniel Carrasco Anthony Acosta Izzudeen Hack Christian Vivas Jorge Quesada Tiffany Hall. EEL-2880 (Section U01 – Fall 2010). General Objectives. Define EOF and Fseek in C Outline parts of 3 programs that use EOF and Fseek
E N D
EOF and Fseek in C ProgrammingGroup #7 Members Daniel Carrasco Anthony Acosta Izzudeen Hack Christian Vivas Jorge Quesada Tiffany Hall • EEL-2880 (Section U01 – Fall 2010)
General Objectives • Define EOF and Fseek in C • Outline parts of 3 programs that use EOF and Fseek • Use block diagrams to explain their structures • Describe the code in detail • Describe particular code functions used • Show how EOF and Fseek are useful in C programming
Problem ExplanationWhat is EOF and Fseek? • EOF (End of File): condition where no more data can be read from data source by the OS • Fseek is a function in the <stdio.h> library that changes the file position indicator for the specified stream or file • “Fseek64” would be used for larger offsets EOF: this reflective strip represents the physical end of a file in this tape.
EOF Program ExplanationWhat does the program do? • Asks user for the directory of a <.txt> file as the input • User would actually input the directory • “C:\Users\Daniel Carrasco\Desktop\Group #7 Outline.txt” • Searches the system for the specified file • Uses pointers to carry this out • Finds the actual file • Displays the text content of that file within the command prompt window
Output of EOF Program Input Text
What is EOF? EOF stands for end-of-file. End-of-file is a marker on each file that is put into a system-maintained administrative data structure. The marker can also be a specific byte number. The actual value is system dependent, but most of the time -1. Once the end-of-file marker has been reached, the system stops reading data from the source.
Declare standard libraries Flowchart of eof program • Declare value BUFSIZE equal to 100 • Declare character string buf with BUFSIZE elements (100) Can the file be opened? Display “Error opening file.” • Declare character string filename with 20 elements • 0 No • Create pointer FILE *fp Yes Display string value of string buf Display “Enter name of text file to display:” Read file string buf with BUFSIZE length End program Retrieve file
Comment stating what the program is Include standard libraries /* Detecting end-of-file. */ #include <stdio.h> #include <stdlib.h> Declare value of BUFSIZE to be 100 #define BUFSIZE 100 Initiate the main function Declare character string buf with BUFSIZE elements Declare character string filename with 20 elements Declare FILE pointer *fp main() { char buf[BUFSIZE]; char filename[20]; FILE *fp; puts("Enter name of text file to display: "); gets(filename); Displays “Enter name of text file to display:” And retrieves file that user inputs /* Open the file for reading. */ if ( (fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } If the file could not be created, display “Error opening file”
/* Open the file for reading. */ if ( (fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error opening file."); exit(1); } A key thing to notice here is that if file cannot be opened, the return value is a 1 instead of a 0, signifying that the program execution was not successful. One more thing…
Explain The next half of the Code: Retrieve a .txt File from a Location in the Users Computer
Specific Code Funtion: • fgets(buf, BUFSIZE, fp); • "fgets" is a function that can be used to read a line from a file and write a line to a file. • Here, the function “fgets” will use the value stored in the character array to write the line of code into the program
General EOF Usage in CODE!!! • It is used as the return value by many of C's I/O functions when they encounter an end-of-file condition or an error.
/* Random access with fseek(). */ #include <stdio.h> #include <stdlib.h> #include <io.h> #define MAX 50 main() { FILE *fp; int data, count, array[MAX]; long offset; /* Initialize the array. */ for (count = 0; count < MAX; count++) array[count] = count * 10; /* Open a binary file for writing. */ if ( (fp = fopen("RANDOM.DAT", "wb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } /* Write the array to the file, then close it. */ if ( (fwrite(array, sizeof(int), MAX, fp)) != MAX) { fprintf(stderr, "\nError writing data to file."); exit(1); } fclose(fp); /* Open the file for reading. */ if ( (fp = fopen("RANDOM.DAT", "rb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } /* Ask user which element to read. Input the element */ /* and display it, quitting when -1 is entered. */ 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; /* Move the position indicator to the specified element. */ if ( (fseek(fp, (offset*sizeof(int)), SEEK_SET)) != NULL) { fprintf(stderr, "\nError using fseek()."); exit(1); } /* Read in a single integer. */ fread(&data, sizeof(int), 1, fp); printf("\nElement %ld has value %d.", offset, data); } fclose(fp); } Block Diagram
/* Random access with fseek(). */ #include <stdio.h> #include <stdlib.h> #include <io.h> #define MAX 50 main() { FILE *fp; int data, count, array[MAX]; long offset; /* Initialize the array. */ for (count = 0; count < MAX; count++) array[count] = count * 10; /* Open a binary file for writing. */ if ( (fp = fopen("RANDOM.DAT", "wb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } /* Write the array to the file, then close it. */ if ( (fwrite(array, sizeof(int), MAX, fp)) != MAX) { fprintf(stderr, "\nError writing data to file."); exit(1); } fclose(fp); /* Open the file for reading. */ if ( (fp = fopen("RANDOM.DAT", "rb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); } /* Ask user which element to read. Input the element */ /* and display it, quitting when -1 is entered. */ 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; /* Move the position indicator to the specified element. */ if ( (fseek(fp, (offset*sizeof(int)), SEEK_SET)) != NULL) { fprintf(stderr, "\nError using fseek()."); exit(1); } /* Read in a single integer. */ fread(&data, sizeof(int), 1, fp); printf("\nElement %ld has value %d.", offset, data); } fclose(fp); } General Structure
Detail /* Random access with fseek(). */ #include <stdio.h> #include <stdlib.h> #include <io.h> #define MAX 50 main() { FILE *fp; int data, count, array[MAX]; long offset; /* Initialize the array. */ for (count = 0; count < MAX; count++) array[count] = count * 10; /* Open a binary file for writing. */ if ( (fp = fopen("RANDOM.DAT", "wb")) == NULL) { fprintf(stderr, "\nError opening file."); exit(1); }
The file RANDOM.DAT is open for reading in binary format. If for any reason it cannot then it prints the message “Error opening file” and exits the program. This while loop has the program print “Enter element to read” and gives two options; the user can either enter an integer between 0-%d which in this case would be 0-49, or -1 which exits the program. If the user decides to enter a number that is bigger than 49 the loop will continue and ask once again to “Enter element to read”
Here the function fseek changes the position indicator to a specified element which in this case would be the offset that was entered *sizeof(int). The “If” statement gives an error message if the return value is not equal to NULL. The SEEK_SET specifies that the position indicator will be moved from the Beginning of the File. This fread reads an array of the number of elements specified (in this case it is 1 element) and each element has a size in bites of sizeof(int) then it prints out the value of that element accordingly
Facts about Fseek • The fseek function positions to the requested byte position within the referenced file. • It disregards characters or their encoding making up the data of the file. • Fseek can not only offset from the beginning of the file (SEEK_SET) but it can also offset from the current position (SEEK_CUR) and also the end of the file (SEEK_END)
Function of Fseek • Fseek can be a very powerful tool, for example in this function fseek is used to change the conventional “Hello World”. The fseek moves the indicator to element number 6 which in this case is “HelloWorld” and replaces the “World” with the word “Class.” When the code is successfully executed the file example.txt reads “Hello Class”
This example uses fseek to write numbers in reverse order. It also has odd numbers in numerical order. It also takes the number 4 and replaces it with 100. Explanation
This line says to read the 12 bytes from file f, which is the current location of the pointer, to the address &r. fwrite then moves the block of bytes from memory to the file. fseek moves the file pointer to a byte in the file. How Fseek is Used
Elements required for fseek Implement objectives Applied to alternate example Lessons Learned http://office.microsoft.com/en-us/images/results.aspx?qu=spiral#ai:MP900402243|
References • http://www.cplusplus.com/reference/clibrary/cstdio/fseek/ • http://en.wikipedia.org/wiki/Fseek • http://www.opengroup.org/onlinepubs/009695399/functions/fseek.html