Normally if I wanted to send some data to the screen (the console
or stdout),
I'd use the printf function.
Here's an example.
printf("There
are %d polar bears outside your window.", BearCount);
It works great. And if I wanted to send the same information
to a file, I would use the fprintf function. It works the same way except you must
specify a file to use. Such as:
// Declare pointer to the file.
FILE* pFileHandle;
// Saves the file in the EXE folder.
char Filename[] = "MyFile.txt";
// Open file for writing.
pFileHandle = fopen(Filename, "w");
// Write data to file.
fprintf(pFileHandle , "("There
really are %d polar bears outside your window.\n\n", BearCount);
// Close the file.
int iReturn = 0;
iReturn = fclose(pFileHandle);
What if I wanted to send the data to the screen or a file or
both? While the standard I/O library creates a type FILE for handling files, it
also creates the objects stdin and stdout. I can use the same fprintf
function in both cases and write code to change the value of pFileHandle.
For a file,
pFileHandle = fopen(…
but for the screen use:
pFileHandle = stdout;
fprintf(pFileHandle , "Print some data to a text
file.\n\n");
OR
fprintf(stdout,
"Print some data to stdout.\n\n");
The same principle applies to reading data from a file or
from the keyboard (i.e., stdin). I can use the fgets function for either.
From a file with,
fgets(Text, num, pFileHandle);
or from the standard input:
fgets(Text, num, stdin);
Data to a maximum length of num is stored in the character
array Text.
Test Code.
I tested the functions in Visual C++ 2010 as an console
application.
// The
standard library includes the system function.
#include <cstdlib>
// C++
standard I/O library.
#include <cstdio>
//
Function prototypes.
int DataOut(FILE*
pFile);
int DataIn(FILE* pFile,
char* text);
// Max
length of text input.
const int MAXLEN = 80;
int main()
{
// Using stdin
& stdout in C & C++
// Header.
printf("Using
stdin & stdout instead of a File\n\n\n");
// Output.
printf("*****
OUTPUT *****\n\n");
// Declare
pointer to the file.
FILE* pFileHandle;
// Saves the file
in the EXE folder.
char
Filename[] = "MyFile.txt";
// Open file.
pFileHandle = fopen(Filename, "w");
// Write data to
file.
DataOut(pFileHandle);
// Close the
file.
int iReturn
= 0;
iReturn = fclose(pFileHandle);
// Do the same
but send to stdout.
DataOut(stdout);
printf("\n\n");
// Input.
printf("*****
INPUT *****\n\n");
// Declarations.
char
readline[MAXLEN];
// Open file.
pFileHandle = fopen(Filename, "r");
// Get data.
DataIn(pFileHandle, readline);
// Close the
file.
iReturn = fclose(pFileHandle);
printf("From
the file: %s\n", readline);
printf("Input
with fgets: ");
// Get data.
pFileHandle = stdin;
DataIn(pFileHandle, readline);
// OR
DataIn(stdin, readline);
printf("Your
input: %s\n", readline);
// Keep console
window open.
system("pause");
// Return some
value.
return 0;
} // end main
int DataOut(FILE*
pFile)
{
// Code to send
data to file or screen.
fprintf(pFile, "Print
some data to a text file or to the screen.\n\n");
return 0;
}
int DataIn(FILE* pFile,
char* text)
{
// Code to send
data to file or screen.
fgets(text, MAXLEN, pFile);
return 0;
}
Output.