Open, read, write and close a text file, block by block using fopen()/fopen_s(), fread() and fwrite() 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, reading, writing and closing a text file, block by block using fopen()/fopen_s(), fread() and fwrite() C functions

To show: How to open, read, write and close a text file, block by block using fopen()/fopen_s(), fread() and fwrite() C functions

 

Create two text files named testfive.txt (this file is optional because of the "w" mode. The "w" mode will create a file if it is not exist) and testsix.txt at C:. Write the following text or any text in the testsix.txt file and save it.

 

 

OPENING, READING AND WRITING ONE BLOCK OF DATA

-----------------------------------------------

This is file testsix.txt. Its content will be

read and then output to the screen/console and

copied to testfive.txt file. The reading and

writing based on block of data. May be this

method is faster compared to read/write by

character, by line.....

-----------------------------------------------

------------------END--------------------------

 

 

Then, run the following program.

 

// Reading and writing one block at a time

#include <stdio.h>

 

// declare enum data type

enum {SUCCESS, FAIL, MAX_LEN = 80};

 

// function prototype for block reading and writing

void BlockReadWrite(FILE *fin, FILE *fout);

// function prototype for error messages...

int ErrorMsg(char *str);

 

int main(void)

{

FILE *fptr1, *fptr2;

errno_t err = 0, err1 = 0;

 

// define the filenames, the files location is at c:\Temp. Other location please provide full path

char filename1[] = "c:\\testfive.txt";

char filename2[] = "c:\\testsix.txt";

int reval = SUCCESS;

 

// test opening testfive.txt file for writing, if fail...

// err = fopen(filename1, "w");, using secure version

err = fopen_s(&fptr1, filename1,"w");

err1 = fopen_s(&fptr2, filename2,"r");

if(err != 0)

{

reval = ErrorMsg(filename1);

}

// test opening testsix.txt file for reading, if fail...

else if (err1 != 0)

{

reval = ErrorMsg(filename2);

}

// if opening files for writing and reading is successful, do...

else

{

// call function for reading and writing

BlockReadWrite(fptr2, fptr1);

// close both files streams...

if(fclose(fptr1)==0)

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

if(fclose(fptr2)==0)

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

}

printf("\n");

return reval;

}

 

// function definition for block read, write

void BlockReadWrite(FILE *fin, FILE *fout)

{

// int num;

size_t num;

char buff[MAX_LEN + 1];

 

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

// while not end of file for input file, do...

while(!feof(fin))

{

// reading...

num = fread(buff, sizeof(char), MAX_LEN, fin);

// append a null character

buff[num * sizeof(char)] = '\0';

printf("%s", buff);

// writing...

fwrite(buff, sizeof(char), num, fout);

}

}

 

// function definition for error message

int ErrorMsg(char *str)

{

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

// display the error message...

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

return FAIL;

}

 

Output example:

 

I'm in BlockReadWrite() doing my job...

OPENING, READING AND WRITING ONE BLOCK OF DATA

-----------------------------------------------

This is file testsix.txt. Its content will be

read and then output to the screen/console and

copied to testfive.txt file. The reading and

writing based on block of data. May be this

method is faster compared to read/write by

character, by line.....

-----------------------------------------------

------------------END--------------------------

c:\testfive.txt successfully closed

c:\testsix.txt successfully closed

Press any key to continue . . .

 

 

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