100% found this document useful (1 vote)
221 views5 pages

File Handling in C++

The document describes several C++ programs and functions that perform operations on text files: 1) A program that writes the numbers 1 to 100 to a text file. 2) A program that writes a string to a text file. 3) Functions that count the number of alphabets, blank spaces, words, and occurrences of the word "the" in a text file. 4) Functions that count the number of lines not starting with A, copy the contents of a file to another file in uppercase letters, and copy words starting with vowels to another file.

Uploaded by

Pace Infotech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
221 views5 pages

File Handling in C++

The document describes several C++ programs and functions that perform operations on text files: 1) A program that writes the numbers 1 to 100 to a text file. 2) A program that writes a string to a text file. 3) Functions that count the number of alphabets, blank spaces, words, and occurrences of the word "the" in a text file. 4) Functions that count the number of lines not starting with A, copy the contents of a file to another file in uppercase letters, and copy words starting with vowels to another file.

Uploaded by

Pace Infotech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ Files and Streams

C++ program to write number 1 to 100 in a data file NOTES.TXT

#include<fstream.h>

int main()
{
ofstream fout;
fout.open("NOTES.TXT");
for(int i=1;i<=100;i++)
fout<<i<<endl;
fout.close();
return 0;
}

C++ program, which initializes a string variable and outputs the string to the disk file

#include<fstream.h>

int main()
{
ofstream fout;
fout.open("out.txt");
char str[300]="Time is a great teacher but unfortunately it kills
all its pupils. Berlioz";
fout<<str;
fout.close();
return 0;
}

User-defined function in C++ to read the content from a text file OUT.TXT, count and
display the number of alphabets present in it

void alphabets()
{
ifstream fin;
fin.open("out.txt");
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(isalpha(ch))
count++;
}
cout<<"Number of alphabets in file are "<<count;
fin.close();
}

User defined function in C++ to count the number of blank present in a text file named
"OUT.TXT".

void blankspace()
{
ifstream fin;
fin.open("out.txt");
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(ch==' ')
count++;
}
cout<<"Number of blank spaces in file are "<<count;
fin.close();
}

User defined function in C++ to count number of words in a text file named "OUT.TXT"

void countwords()
{
ifstream fin;
fin.open("out.txt");
char word[30];
int count=0;
while(!fin.eof())
{
fin>>word;
count++;
}
cout<<"Number of words in file are "<<count;
fin.close();
}
User defined function in C++ to print the count of word the as an independent word in a
text file STORY.TXT

Write a function in C++ to print the count of word the as an independent word in a text file
STORY.TXT.
for example, if the content of the file STORY.TXT is
There was a monkey in the zoo. The monkey was very naughty.

Then the ouput of the program should be 2.

void countword()
{
ifstream fin;
fin.open("STORY.TXT");
char word[30];
int count=0;
while(!fin.eof())
{
fin>>word;
if(strcmpi(word,"the")==0)
count++;
}
cout<<"Number of the word in file are "<<count;
fin.close();
}

Write a function in C++ to count and display the number of lines not starting with alphabet 'A'
present in a text file "STORY.TXT".
Example:
If the file "STORY.TXT" contains the following lines,
The rose is red.
A girl is playing there.
There is a playground.
An aeroplane is in the sky.
Numbers are not allowed in the password.

The function should display the output as 3

void countlines()
{
ifstream fin;
fin.open("STORY.TXT");
char str[80];
int count=0;
while(!fin.eof())
{
fin.getline(str,80);
if(str[0]!='A')
count++;
}
cout<<"Number of lines not starting with A are "<<count;
fin.close();
}

Assuming that a text file named FIRST.TXT contains some text written into it, write a function
named copyupper(), that reads the file FIRST.TXT and creates a new file named SECOND.TXT
contains all words from the file FIRST.TXT in uppercase.
void copyupper()
{
ifstream fin;

fin.open("FIRST.TXT");
ofstream fout;
fout.open("SECOND.TXT");
char ch;
while(!fin.eof())
{
fin.get(ch);
ch=toupper(ch);
fout<<ch;
}
fin.close();
fout.close();
}
Assuming that a text file named FIRST.TXT contains some text written into it, write a function
named vowelwords(), that reads the file FIRST.TXT and creates a new file named
SECOND.TXT, to contain only those words from the file FIRST.TXT which start with a
lowercase vowel (i.e., with 'a', 'e', 'i', 'o', 'u').
For example, if the file FIRST.TXT contains
Carry umbrella and overcoat when it rains
Then the file SECOND.TXT shall contain
umbrella and overcoat it
void vowelwords()
{
ifstream fin;
fin.open("FIRST.TXT");
ofstream fout;
fout.open("SECOND.TXT");
char word[30];
while(!fin.eof())
{
fin>>word;

if(word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u
')
fout<<word<<" ";
}
fin.close();
fout.close();
}

You might also like