Pages

Thursday, October 20, 2011

Copying a file

To copy the contents of a file to another, we will use both get() and put() in the same code. To make this program, we will open two files at the same time , the source file(the one that we want to copy) and the target file(the file that will contain the copied data). The source file will be opened in the read mode(using ifstream) whereas the target file will be accessed in the write mode(using ofstream).

Have a look at the code

#include "iostream.h"
#include "conio.h"
#include "fstream.h"
void main()
{
 clrscr();
 int cnt=0;
 ifstream in("file1.txt"); // source file (must be existing)
 ofstream out("file2.txt"); // target file (will be automatically created)
 char ch;
 in.get(ch); // reading from the source file
 while(in)
 {
  out.put(ch); // writing in the target file
  in.get(ch); // reading the next char from the source file
  cnt++;
 }
 cout<<"\nCopied "<<cnt "<<"" characters="" characters?;="" copied. in.close();
 out.close();
}


Output : (Assuming that the source file has 30 characters)
Copied 30 characters

The above code will open two files - source(file1.txt) and target(file2.txt). The get() will read a character from the first file and store it in the variable "ch". The put() will copy the content of the variable "ch" into the target file. The process will continue till the first file gets completely read.Once the program completes its execution we can open the target file to see if the copy was correct or not.



Word Count Program Using get()

Using the get() we can easily write a program that can count the number of characters in a file. This will be quite similar to the Word Count option available in Ms-Word.


#include "conio.h"
#include " ctype.h" // for character functions
#include "iostream.h"
#include "fstream.h"
void main()
{
 clrscr();
 ifstream in("file1.txt"); // name of any existing file
 int chars=0,caps=0,small=0,num=0,space=0;
 char ch;
 in.get(ch);
 while(in)
 {
  if(isalpha(ch))
  {
   chars++;
   if(isupper(ch))
    caps++;
   else if(islower(ch))
    small++;
  }
  else if (isdigit(ch))
   num++;
  else if(isspace(ch))
   space++;
  in.get(ch);
 }
 cout<<"\nStatistics ";
 cout<<"\n============";
 cout<<"\nTotal Chars : "<<chars;
 cout<<"\nCapital Letters : "<<caps;
 cout<<"\nSmall Letters : "<<small;
 cout<<"\nDigits : "<<num;
 cout<<"\nSpaces : "<<space;
 in.close();

}

Output
Statistics
==========
Total Chars : 40
Capital Letters : 20
Small Letters : 10
Digits : 5
Spaces : 5

The above code will first open the file in read mode and start reading the file one char at a time using the "get()". The read char will be checked to see whether it is a capital or small letter, a digit or a space. Wehave declared different counter variables to count each type of char, for example the variable "caps" will count the capital letters while "small" is used to check small letters. To check a character, the character functions "isupper()","islower()" have been used. These are boolean functions and return their value in true/false. We can also use manual comparison statements like "if(ch>='A' && ch<='Z') etc.

Tuesday, October 18, 2011

Reading a complete file using get()

Reading the whole file using get()

The get() can not read more than one character at a time , therefore we must use a loop to read the file. The while loop is most suitable for reading a file since we don't know how much data is there in the file.

#include "conio.h"
#include "iostream.h"
#include "fstream.h"
void main()
{
 clrscr();
 ifstream in("file1.txt");
 char c;
 in.get(c);
 while(in)
 {
  cout<<c;
  in.get(c);
 }
 in.close();
}

Output (Assuming that the file contains a word "Welcome")
Welcome

The while loop will run as long as the file contains the data. The get() function will read all the contents of the file till the its end.

Reading a file - char by char (using get())

The get() function

The get() can read a char from a file. It is used when we want to read a single character from a file. To read all the data of a file, the same function can be enclosed inside a loop.

Syntax
ifstream.get(char);

Example : Lets see the following code snippet that makes use of the "get()" to read a char from the file.

#include "conio.h"
#include "iostream.h"
#include "fstream.h"

void main()
{
 clrscr();
 ifstream in("file1.txt");
 char c;
 in.get(c);
 cout<<c;

in.close();
}


The above program will open a file named "file1.txt" in read mode using the ifstream class. Once the file is opened the "get()" will read a character from the file. Since a file always opens from the beginning, the code will read the first character of the file only.

Output (Assuming the file contains a word "Welcome")
W


Writing a string using put()

Using the put() to write a string

To write multiple characters in a file using the "put()", we can use the function inside a loop.  Without a loop it is not possible to write a complete string with put(), since the put() can not write more than one character at a time,

#include "conio.h"
#include "iostream.h"
#include "fstream.h"
#include "string.h"
void main()
{
 clrscr();
 ofstream of("file1.txt");
 char str[20];
 cout<<"\nEnter a string  : ";
 cin>>str;
 for(int i=0;i <strlen(str);i++)
of.put(str[i]);

cout<<"Saved";
 of.close();
}


The above program will ask the user to write a string that will be saved in the variable "str[]". A "for" loop has been used to write all the characters of the array from 0 to the length of the string. Inside the loop we have used the statement "of.put(str[i])", that will write the value of all the characters of the array in the file one char at a time. For using the "strlen()" we have used the header file "string.h".

Output
Enter a string : Programming
Saved

To view the file you can use the same process as described in the previous post.

Writing a file (char by char)

To read a file one char at a time, we can use the "put()". The put() writes a character to a file. To write multiple characters we can either use the put() inside a loop or we can use functions that read the data string by string.

Syntax
ofstream.put(char);

Example 1 : Lets see how the put() can be used to write a single character in the file.

#include "conio.h"
#include "fstream.h"
#include "iostream.h"
void main()
{
 clrscr();
 ofstream of("file1.txt");
 char c;
 cout<<"\nEnter a character to be stored in the file  : ";
 cin>>c;

cout<<"\nSaved";
 of.put(c);
 of.close();
}


When you run the program, it asks for a character that you want to store or save in the file. The character that you enter will be stored in the file named "file1.txt", that you can open in My Computer to verify.

Output
Enter a character to be stored in the file : P
Saved

To verify the writing process.
Open the file "file1.txt" using My Computer or click "DOS Shell" and type "type file1.txt" at the command prompt.