Open and read file content randomly and display it using fopen()/fopen_s(), fseek(), ftell() and fgets() C functions

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows Xp Pro with SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Opening and reading file content randomly and display it using fopen()/fopen_s(), fseek(), ftell() and fgets() C functions

To show: How to access (open and read) a text file randomly using fopen()/fopen_s(), fseek(), ftell() and fgets() C functions

 

Create a text files named testfive.txt at C:. Write the following text or any text and save it.

 

 

THIS IS THE FIRST LINE OF TEXT, tesseven.txt file

THIS IS THE SECOND LINE OF TEXT, tesseven.txt file

THIS IS THE THIRD LINE OF TEXT, tesseven.txt file

THIS IS THE FOURTH LINE OF TEXT, tesseven.txt file

 

 

Then, run the following program.

 

// Random access to a file

#include <stdio.h>

 

enum {SUCCESS, FAIL, MAX_LEN = 120};

 

// function prototypes, seek the file position indicator

void PtrSeek(FILE *fptr);

// function prototype, tell the file position indicator

long PtrTell(FILE *fptr);

// function prototype for read and writes

void DataRead(FILE *fptr);

// function prototype for error message

int ErrorMsg(char *str);

 

int main(void)

{

// FILE pointer

FILE *fptr;

// Initially set to file can be found

errno_t err = 0;

// Other location should provide the full path

char filename[] = "c:\\tesseven.txt";

int reval = SUCCESS;

 

// If there is some error opening file for reading

// fopen(filename, "r"); using a secure version...

err = fopen_s(&fptr, filename, "r");

if(err != 0)

{

// Then handle the error of file not found/can't be opened

reval = ErrorMsg(filename);

}

// if opening is successful

else

{

// PtrSeek() function call

PtrSeek(fptr);

 

// close the file stream

if(fclose(fptr)==0)

printf("%s successfully closed.\n", filename);

}

return reval;

}

 

// PtrSeek() function definition

void PtrSeek(FILE *fptr)

{

long offset1, offset2, offset3, offset4;

 

printf("I\'m in PtrSeek(), doing my job...\n");

offset1 = PtrTell(fptr);

DataRead(fptr);

offset2 = PtrTell(fptr);

DataRead(fptr);

offset3 = PtrTell(fptr);

DataRead(fptr);

offset4 = PtrTell(fptr);

DataRead(fptr);

printf("\nReread the tesseven.txt, in random order:\n");

 

// re-read the 2nd line of the tesseven.txt

fseek(fptr, offset2, SEEK_SET);

DataRead(fptr);

// re-read the 1st line of the tesseven.txt

fseek(fptr, offset1, SEEK_SET);

DataRead(fptr);

// re-read the 4th line of the tesseven.txt

fseek(fptr, offset4, SEEK_SET);

DataRead(fptr);

// re-read the 3rd line of the tesseven.txt

fseek(fptr, offset3, SEEK_SET);

DataRead(fptr);

}

// PtrTell() function definition

long PtrTell(FILE *fptr)

{

long reval;

// tell the fptr position

reval = ftell(fptr);

 

printf("I\'m in PtrTell(), telling you the fptr position\n");

printf("The fptr is at %ld\n", reval);

return reval;

}

 

// DataRead() function definition

void DataRead(FILE *fptr)

{

char buff[MAX_LEN];

printf("I\'m in DataRead() doing my job\n");

//reading line of text at the fptr position

fgets(buff, MAX_LEN, fptr);

//and display the text

printf("-->%s\n", buff);

}

 

// Error message function definition

int ErrorMsg(char *str)

{

// display this error message

printf("I\'m in ErrorMsg(), handling error(s) as needed\n");

printf("Problem, cannot open %s.\n", str);

return FAIL;

}

 

Output example:

 

I'm in PtrSeek(), doing my job...

I'm in PtrTell(), telling you the fptr position

The fptr is at 0

I'm in DataRead() doing my job

-->THIS IS THE FIRST LINE OF TEXT, tesseven.txt file

I'm in PtrTell(), telling you the fptr position

The fptr is at 51

I'm in DataRead() doing my job

-->THIS IS THE SECOND LINE OF TEXT, tesseven.txt file

I'm in PtrTell(), telling you the fptr position

The fptr is at 103

I'm in DataRead() doing my job

-->THIS IS THE THIRD LINE OF TEXT, tesseven.txt file

I'm in PtrTell(), telling you the fptr position

The fptr is at 154

I'm in DataRead() doing my job

-->THIS IS THE FOURTH LINE OF TEXT, tesseven.txt file

 

Reread the tesseven.txt, in random order:

I'm in DataRead() doing my job

-->THIS IS THE SECOND LINE OF TEXT, tesseven.txt file

I'm in DataRead() doing my job

-->THIS IS THE FIRST LINE OF TEXT, tesseven.txt file

I'm in DataRead() doing my job

-->THIS IS THE FOURTH LINE OF TEXT, tesseven.txt file

I'm in DataRead() doing my job

-->THIS IS THE THIRD LINE OF TEXT, tesseven.txt file

c:\tesseven.txt successfully closed.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index