Opening file, reading and writing one line at a time using fputs()/fputws(), fopen()/fopen_s() and fgets()/fgetws() 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: Here, the secure versions of the functions used. If the older one used, do expect warning.

To do: Opening file, reading and writing one line at a time using fputs()/fputws(), fopen()/fopen_s() and fgets()/fgetws() C functions

To show: How to use fputs()/fputws(), fopen()/fopen_s() and fgets()/fgetws() C functions to open, read and write one line at a time

 

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

 

OPENING, READING, WRITING one line of characters

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

This is file testfour.txt. This file's content will

be read line by line of characters till no more line

of character found. Then, it will be output to the

screen and also will be copied to file testhree.txt.

Check the content of testhree.txt file...

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

------------------HAVE A NICE DAY-------------------

 

 

Then, run the following program.

 

 

// Reading and writing one line at a time

#include <stdio.h>

#include <stdlib.h>

 

enum {SUCCESS, FAIL, MAX_LEN = 100};

 

// function prototype for read and writes by line...

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

 

int main(void)

{

FILE *fptr1, *fptr2;

errno_t err = 0, err1 = 0;

// file testhree.txt is located at the root, c:. You can put this file at any location provided you provide the full path, same for testfour.txt

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

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

char reval = SUCCESS;

 

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

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

// test opening testfour.txt file for reading

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

if(err != 0)

{

printf("Problem, cannot open %s for writing.\n", filename1);

reval = FAIL;

}

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

else if(err1 != 0)

{

printf("Problem, cannot open %s for reading.\n", filename2);

reval = FAIL;

}

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

else

{

// function call for read and write, line by line...

LineReadWrite(fptr2, fptr1);

// close both files stream...

if(fclose(fptr1)==0)

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

if(fclose(fptr2)==0)

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

}

return reval;

}

// function definition for line read, write

void LineReadWrite(FILE *fin, FILE *fout)

{

// local variable...

char buff[MAX_LEN];

 

while(fgets(buff, MAX_LEN, fin) !=NULL)

{

// write to a file...

fputs(buff, fout);

// write to a screen...

printf("%s", buff);

}

}

 

Output example:

 

OPENING, READING, WRITING one line of characters

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

This is file testfour.txt. This file's content will

be read line by line of characters till no more line

of character found. Then, it will be output to the

screen and also will be copied to file testhree.txt.

Check the content of testhree.txt file...

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

------------------HAVE A NICE DAY-------------------

c:\testhree.txt successfully closed.

c:\testfour.txt successfully closed.

Press any key to continue . . .

 

 

Check the content of the testhree.txt. It should be similar to the testfour.txt content. Then, change the content of the testfour.txt, save it and re-run the program and re-check the testhree.txt content.

 

 

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