www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 36: Efficient File Handling Bookmark and Share
Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
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
}



NEXT>>>>>Lesson 37. Structures



Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.