1 / 22

Fseek and EOF (End Of File)

Fseek and EOF (End Of File). Group 4 Daniel MacKinnon Jonathan Nicholas Willians Alcerro Adrian Alvarez Angelica Silva Ricardo Viera. Purpose of Fseek.

alize
Download Presentation

Fseek and EOF (End Of File)

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. Fseek and EOF (End Of File) Group 4 Daniel MacKinnon Jonathan Nicholas Willians Alcerro Adrian Alvarez Angelica Silva Ricardo Viera

  2. Purpose of Fseek • Our program is designed to write an array to a file, then reopen the file for reading. The array will already be a part of the file before the user prompt appears asking for input on which element of the array to read, and using fseek we will read, and then display back to the user what value it is they were looking for in the array.

  3. The program Fseek introduces new file input/output techniques that help us write to a file and also read from a file. • The fseek function moves a position indicator by using its parameters. A typical fseek function looks like this : • fseek (FILE *stream, long offset, int whence)

  4. fseek (FILE *stream, long offset, int whence) • Where FILE *stream indicates the file that you are using (with a file pointer), • Long offset indicates the position from the origin(which in our program is created by user input) that the indicator will be moved, • And ‘int whence’ which indicates the placement of the origin. There are 3 separate origins, which can be used depending on what you are trying to accomplish: • SEEK_SET : Sets the origin to the very beginning • SEEK_CUR : Sets the origin to the current existing position • SEEK_END : Sets the origin to the end

  5. Calling fseek will clear any end-of-file indicator for the stream as well. • Although not used in this example, fseek is a tool to determine the size of a file by using SEEK_END to move the position indicator to the end of the program and with an offset of zero, then use ftell to let the user know what the current position in the stream is. Please note that this only works in binary mode, which is achieved by adding the ‘b’ after w, or r, or whichever parameter you used when calling fopen.

  6. Create an array with 50 elements Set the value of each element 10 times itself. Write the array to RANDOM.DAT Open the file again for reading its contents. Save file, then close it. Ask the user which element they want to read. Use fseek to read the desired element. Print the value of the specified element. Allow user to read multiple elements or exit the program.

  7. Declares intx,y,z Declares the file pointer as *ptr_myfile Opens test.bin with the parameters RB which means read in binary mode. If there is an error the user is notified with a printf statement. Inside this for loop, we are looking at the most recent record in the file. The fread function reads and the printf will display the record. This program is useful for displaying the most recent information, and counting backwards. The for loop decrement enables us to do this.

  8. EOF and File I/O Indicating whether the End-of-File has been reached or if an error occurs while reading a file(stream)

  9. Purpose of EOF • Asks a user which file they would like to have read, and if the end of file has not been reached, print back to the user the contents of the file line by line

  10. Asks user which file to open Error message Opens file If text file doesn’t exists If it exists Prints out content of file Exits the program

  11. Define globally buffer size to be 100 Declares char array buf to be of the size BUFSIZE Declares char array filename Declares file pointer Prints to the user to a command to input what file they want displayed Gets retrieves the input and assigns it to filename Using fopen, we open the file for reading with the r parameter If there is an error, the program will display this error message to the user If the end of file is not reached, the program uses fget to retrieve the line and the printf statement prints the string (%s) Fclose closes the file.

  12. #include <stdio.h> int main(void) { char buffer[256]; FILE * myfile; myfile = fopen("some.txt","r"); while (!feof(myfile)) { fgets(buffer,256,myfile); printf("%s",buffer); } fclose(myfile); return 0; Declares a string ‘buffer’ with a length of 256 Declares the file pointer Uses fopen with the read parameter to open Some.txt In this while statement, if the end of file is Not reached the program will print back the Text of the file Fclose closes the file, Return ends the program

More Related