Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez
This presentation is the property of its rightful owner.
Sponsored Links
1 / 20

EOF & Fseek Gustavo Olmeta Cindy Gonzalez Ana Arias Jeffrey Barajas Frank Rodriguez PowerPoint PPT Presentation


  • 52 Views
  • Uploaded on
  • Presentation posted in: General

EOF & Fseek Gustavo Olmeta Cindy Gonzalez Ana Arias Jeffrey Barajas Frank Rodriguez. EOF program. Detecting end of file. Definition feof (): is used to determine whether the end-of-file indicator is set for the file associated to stdin . Returns a nonzero value if it is.

Download Presentation

EOF & Fseek Gustavo Olmeta Cindy Gonzalez Ana Arias Jeffrey Barajas Frank Rodriguez

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

EOF & FseekGustavo OlmetaCindy Gonzalez Ana AriasJeffrey Barajas Frank Rodriguez


Eof program

EOF program

Detecting end of file


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

Definition

feof(): is used to determine whether the end-of-file indicator is set for the file associated to stdin. Returns a nonzero value if it is.

It’s often used as a condition in a while loop to keep the program reading the file until the end-of-file indicator is reached.

Function prototype:

intfeof(FILE *fptr)


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

Purpose

The End of File program opens, reads and displays a user specified text file line by line until the end of file indicator is reached.


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

Outline and Flowchart

  • Create Variables

  • #define BUFSIZE 100

  • char buf[BUFSIZE]

  • char filename[20]

  • FILE *fp

  • User interface

  • puts(“Enter name of text file to display:”)

  • gets(filename)

  • Open file

  • fp = fopen(filename, “r”)) == NULL)

  • Read and display until EOF

  • while (!feof(fp)) {fgets(buf, BUFSIZE, fp);printf(“%s”, buf); }


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

1-/* Detecting end-of-file. */

2-#include <stdio.h>

3-#include <stdlib.h>

4-#define BUFSIZE 100

5-

6-main()

7-{

8-char buf[BUFSIZE];

9-char filename[20];

10-FILE *fp;

11-

12-puts("Enter name of text file to display: ");

13-gets(filename); /* Open the file for reading. */

14-

15-if ( (fp = fopen(filename, "r")) == NULL)

16-{

17- fprintf(stderr, "Error opening file.");

18- exit(1);

19-}

20-

21-/* If end of file not reached, read a line and display it. */

22-while ( !feof(fp) )

23-{

24- fgets(buf, BUFSIZE, fp);

25- printf("%s",buf); } fclose(fp);

26-}


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

Sample file


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

Sample output


Another eof example

Another EOF example

/* feof example: byte counter */

#include <stdio.h>

int main ()

{

FILE * pFile;

long n = 0;

pFile = fopen ("myfile.txt","rb");

if (pFile==NULL)

perror ("Error opening file");

else

{

while (!feof(pFile))

{

fgetc (pFile); n++;

}

fclose (pFile);

printf ("Total number of bytes: %d\n", n-1);

}

return 0;

}


File function program

File Function Program

Random access with fseek()


Definition

Definition

fseek() sets the file position indicator associated with file stream according to the values of offset and origin and supports random access I/O operations

  • Function prototype:

    • intfseek(FILE *stream, long int offset, int origin)

  • Values for origin must be one of the 3 following macros

    • SEEK_SET: seek from start of file

    • SEEK_CUR: seek from current position

    • SEEK_END: seek from end of file

  • Return value of 0 implies success, nonzero value indicates failure


Purpose

Purpose

The Random access with fseek program creates a binary file, writes on it, reopens it and looks for specific elements within the data as requested by user input.


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

Outline and Flowchart:

  • Create file

  • fp = fopen(“RANDOM.DAT”, “wb”)

  • Write to file

  • fwrite(array, sizeof(int), MAX, fp)

  • Read file

  • fp = fopen(“RANDOM.DAT”, “rb”)

  • Seek and print file element

  • fseek(fp, (offset*sizeof(int)), SEEK_SET)

  • fread(&data, sizeof(int), 1, fp)


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

1-/* Random access with fseek(). */

2-#include <stdio.h>

3-#include <stdlib.h>

4-#include <io.h>

5-

6-#define MAX 50

7-

8-main()

9-{

10-FILE *fp;

11-int data, count, array[MAX];

12-long offset;

13-

14-/* Initialize the array. */

15-for (count = 0; count < MAX; count++)

16-array[count] = count * 10;

17-

18-/* Open a binary file for writing. */

19-if ( (fp = fopen("RANDOM.DAT", "wb")) == NULL)

20-{

21- fprintf(stderr, "\nError opening file.");

22- exit(1);

23-}

24-


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

25-/* Write the array to the file, then close it. */

26-if ( (fwrite(array, sizeof(int), MAX, fp)) != MAX)

27-{

28- fprintf(stderr, "\nError writing data to file.");

29- exit(1);

30-}

31-fclose(fp);

32-

33-/* Open the file for reading. */

34-if ( (fp = fopen("RANDOM.DAT", "rb")) == NULL)

35-{

36- fprintf(stderr, "\nError opening file.");

37- exit(1);

38-}


Eof fseek gustavo olmeta cindy gonzalez ana arias jeffrey barajas frank rodriguez

40-/* Ask user which element to read. Input the element entered. */

41-while (1)

42-{

43- printf("\nEnter element to read, 0-%d, -1 to quit: ",MAX-1);

44- scanf("%ld", &offset);

45- if (offset < 0) break; else if (offset > MAX-1) continue;

46-

47- /* Move the position indicator to the specified element. */

48- if ( (fseek(fp, (offset*sizeof(int)), SEEK_SET)) != NULL)

49- {

50-fprintf(stderr, "\nError using fseek().");

51-exit(1);

52- }

53-

54- /* Read in a single integer. */

55- fread(&data, sizeof(int), 1, fp);

56- printf("\nElement %ld has value %d.", offset, data);

57- }

58-

59-fclose(fp);

60-}


Output

Output


Another fseek example

Another Fseek Example

/* fseek example */

#include <stdio.h>

int main ()

{

FILE * pFile;

pFile = fopen ( "example.txt" , "w" );

fputs ( "This is an apple." , pFile );

fseek ( pFile , 9 , SEEK_SET );

fputs ( " sam" , pFile ); fclose ( pFile );

return 0;

}


Questions or comments

Questions or comments?


Thank you for your time

Thank you for your time!


  • Login