Reading and writing one character at a time using fopen()/fopen_s(), fgetc()/fgetwc() and putchar()/putwchar() 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: Reading and writing one character at a time using fopen()/fopen_s(), fgetc()/fgetwc() and putchar()/putwchar() C functions

To show: How to use the fopen()/fopen_s(), fgetc()/fgetwc() and putchar()/putwchar() C functions for reading and writing a character at a time

 

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

 

OPENING, READING, WRITING AND CLOSING FILE

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

Testing file. This file named testtwo.txt.

After opening files for reading and writing,

without error, content of this file (testtwo.txt)

will be read and output (write) to the other

file named testone.txt and standard

output(screen/console) character by character!!!

---HAPPY LEARNING FOLKS!!!----

 

Run the following program. This program will try to find both text files. testone.txt will be opened for writing and testtwo.txt will be opened for reading. At the end both text files will have same content.

 

 

// Reading and writing one character at a time

#include <stdio.h>

 

// enumerated data type, SUCCESS = 0, FAIL = 1

enum {SUCCESS, FAIL};

 

// prototype function for reading from and writing to...

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

int main()

{

// declare two file pointers...

FILE *fptr1, *fptr2;

errno_t err = 0, err1 = 0;

 

// define the two files name which located at C:\ drive

char filename1[] = "C:\\testone.txt";

char filename2[] = "C:\\testtwo.txt";

int reval = SUCCESS;

// test the opening filename1 for writing....

// if fails....

// fopen(filename1, "w"), using secure version and for writing if the file does not exist yet, it will be created

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

// fopen(filename2, "r"), using secure version and for reading, if the file does not exist, it will generate an error...

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

if (err != 0)

{

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

reval = FAIL;

}

 

// if opening filename1 for writing is successful, test for opening for reading filename2, if fails...

else if (err1 != 0)

{

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

reval = FAIL;

}

// if successful opening for reading from filename2 and writing to filename1...

else

{

// function call for reading and writing...

CharReadWrite(fptr2, fptr1);

// close both files...

if(fclose(fptr1)==0)

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

if(fclose(fptr2)==0)

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

}

return reval;

}

// read write function definition

void CharReadWrite(FILE *fin, FILE *fout)

{

int c;

 

printf("I'm in CharReadWrite(). Do some reading & writing.\n\n");

// if the end of file is reached, do...

while ((c = fgetc(fin)) != EOF)

{

// write to a file...

fputc(c, fout);

// display on the screen...

putchar(c);

}

printf("\n");

}

 

Output example:

 

Check the content of the testone.txt file. It should has a testtwo.txt content

 

I'm in CharReadWrite(). Do some reading & writing.

OPENING, READING, WRITING AND CLOSING FILE

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

Testing file. This file named testtwo.txt.

After opening files for reading and writing,

without error, content of this file (testtwo.txt)

will be read and output (write) to the other

file named testone.txt and standard

output(screen/console) character by character!!!

---HAPPY LEARNING FOLKS!!!----

C:\testone.txt close successfully

C:\testtwo.txt close successfully

Press any key to continue . . .

 

If the testtwo.txt is not there, the following output should be expected.

 

Problem, cannot open C:\testtwo.txt.

Press any key to continue . . .

 

 

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