HowToProgramC : Lesson 36


Back Home Next



Efficient File Handling

Let’s consider an example. We know how to read a file character by character, write into another file or on the screen. If we want to write into a file after reading another file, there are already enough tools to get (read) one character from a file and put (write) into the other one. We can use inputFile.getc () to get a character and outputFile.putc () to write a character into a file. As mentioned earlier, there is very inefficient way of doing things . We also know that for reading and writing to disk, processing in chunks is more efficient. Can we handle more data than a single byte or a single line? The answer is yes. We can use read () and write () functions for this purpose. These functions are binary functions and provided as a part of the stream functions. The term binary means that they read and write in binary mode, not in characters. We tell a location in memory to read () function to write the read data and with the number of bytes to read or write. Usually, read(arrayname, number of bytes) e.g. read(a, 10). Now depending on our computer’s memory, we can have a very large data in it. It may be 64K.

Sample Code

/* This is a sample program to demonstrate the use of open(), close(), seekg(), get() functions and streams. It expects a file named my-File.txt in the current directory having some data strings inside it. */
#include <fstream.h>
#include <stdlib.h>
/* Declare the stream objects */
ifstream inFile;
ofstream scrn, prnt;
main()
{
char inChar;
inFile.open("my-File.txt", ios::in); // Open the file for input
if(!inFile)
{
cout << "Error opening file"<< endl;
}
scrn.open("CON", ios::out); // Attach the console with the output stream
while(inFile.get(inChar)) // Read the whole file one character at a time
{
scrn << inChar; // Insert read character to the output stream
}
scrn.close(); // Close the output stream
inFile.seekg(0l, ios::beg); // Go to the beginning of the file
prnt.open("LPT1", ios::out);// Attach the output stream with the LPT1 port
while(inFile.get(inChar)) // Read the whole file one character at a time
{
prnt << inChar; // Insert read character to the output stream
}
prnt.close(); // Close the output stream
inFile.close(); // Close the input stream
}

Sample Code 2

/* This sample code demostrates the use of fstream and seekg() function. It will create a file named my-File.txt write alphabets into it, destroys the previous contents */
#include <fstream.h>
fstream rFile; // Declare the stream object
main()
{
char rChar;
/* Opened the file in both input and output modes */
rFile.open("my-File.txt", ios::in || ios::out);
if(!rFile)
{
cout << "error opening file"<< endl;
}
/* Run the loop for whole alphabets */
for ( rChar ='A'; rChar <='Z'; rChar++)
{
rFile << rChar; // Insert the character in the file
}
rFile.seekg(8l, ios::beg); // Seek the beginning and move 8 bytes forward
rFile >>rChar; // Take out the character from the file
cout << "the 8th character is " << rChar ;
rFile.seekg(-16l, ios::end);// Seek the end and move 16 positions backword
rFile >>rChar; // Take out the character at the current position
cout << "the 16th character from the end is " << rChar ;
rFile.close(); // Close the file
}



Back Home Next


Google




learntoknow@yahoo.com
© All rights Reserved.