0% found this document useful (0 votes)
115 views8 pages

Quick Revision in Text Files in C

The document discusses text files in C++ and various operations that can be performed on them. Some key points: - Text files contain ASCII characters and can be read/written using ifstream, ofstream, and fstream classes - Various file modes like ios::in, ios::out, ios::app are used for reading, writing, and appending - Functions are defined to write, add, display, print lines of a text file - Functions count lines, characters, digits, words, vowels and perform other operations on a text file

Uploaded by

Vansh Gahlot
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
0% found this document useful (0 votes)
115 views8 pages

Quick Revision in Text Files in C

The document discusses text files in C++ and various operations that can be performed on them. Some key points: - Text files contain ASCII characters and can be read/written using ifstream, ofstream, and fstream classes - Various file modes like ios::in, ios::out, ios::app are used for reading, writing, and appending - Functions are defined to write, add, display, print lines of a text file - Functions count lines, characters, digits, words, vowels and perform other operations on a text file

Uploaded by

Vansh Gahlot
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/ 8

Quick revision in Text Files in C++

Text file: A file that contains data in the form of ASCII characters. It is readable as well as
printable using notepad. Character translation takes place for eg. \n or endl is converted into a new
line.

ifstream : A class whose objects are used to read from a file.


ofstream : A class whose objects are used to write in a file.
fstream : A class used for both reading and writing.

File Modes:
1. ios::in – for reading from a file
2. ios::out – for writing in a file ( always starts fresh)
3. ios::app – for appending in a file
4. ios::ate – for appending by default and also writing any where in the file.
5. ios::in | ios::out – for reading and writing i.e for file modification
6. There is no explicit mode for a text file

Explanation of few important lines:

1. char ch;
f1.get(ch);
A single character is read from the current position of get pointer in the file f1 into the
character ch.

2. char w[20];
f1>>w;
A word (multiple characters without spaces) is read from the current position of get
pointer in the file f1 into the array w.

3. char s[80];
f1.getline(s,80);
A line (multiple characters with spaces) is read from the current position of get pointer in
the file f1 into the array s.

4. char s[30];
f1<<s;
The array s (multiple characters with/without spaces) is written in the file f1 from the
current position of put pointer.

5. char ch;
f1.put(ch);
The character ch (single character) is written in the file f1 from the current position of put
pointer.

6. f1.seekg(10); or f1.seekg(10,ios::beg);
sets the get pointer on the 10th byte from the beginning of the file, so that reading can take
place from 10th byte onward.

7. f1.seekg(-15,ios::cur);
sets the get pointer on the 15th byte backward from the current position of the get pointer
in the file, so that reading can take place from that byte onward.

8. f1.tellg( );
returns the current position of the get pointer in the file f1.
eg. int pos;
pos=f1.tellg( );

Write the function definitions for the following operations.

1. To write lines of text in a text file “stu.txt” .


2. To add lines of text in a text file “stu.txt”.
3. To display contents of a text file “stu.txt”.
4. To print the content of a text file “stu.txt”.
5. To count the number of lines in a text file “stu.txt”.
6. To count the number of characters in a text file “stu.txt”.
7. To count the number of digits in a text file “stu.txt”.
8. To count the number of capital letters in a text file “stu.txt”.
9. To count the number of words in a text file “stu.txt”.
10. To count the number of vowels in a text file “stu.txt”.
11. To count the number of lines starting with “A” in a text file “stu.txt”.
12. To count the number of words starting with vowels in a text file “stu.txt”.
13. To count the number of occurrences of the word “is” in a text file “stu.txt”.
14. To change the cases in a text file “stu.txt”.
15. To Copy the words starting with an upper case character from a text file “stu.txt” to a file
named “copy.txt”.
16. To delete the words starting with a lower case character from the file “stu.txt”.
17. To search for a word passed as a parameter in the file “stu.txt” and display “successful” if
found, otherwise display “Unsuccessful”;

Ans1 void wtstudent( )


{
ofstream f(“stu.txt”);
char s[80];
char resp;
do
{
cout<<”\nEnter a line : “;
cin.getline(s,80);;
f<<s<<endl;
cout<<”\nWant to enter more (y/n): “;
cin>>resp;
}while(resp==’y’);
f.close( );
}

Ans2 void atstudent( )


{
fstream f;
f.open(“stu.txt”,ios::app);
char s[80];
char resp;
do
{
cout<<”\nEnter a line : “;
cin.getline(s,80);;
f<<s<<endl;
cout<<”\nWant to enter more (y/n): “;
cin>>resp;
}while(resp==’y’);
f.close( );
}

Ans3 void disstudent( ) void disstudent( )


{ {
ifstream f(“stu.txt”); ifstream f(“stu.txt”);
char s[80]; char ch;
while(f.getline(s,80))) or while(f.get(ch)))
cout<<s<<endl; cout<<ch;
f.close( ); f.close( );
} }

Ans4 void prstudent( ) void prstudent( )


{ {
ifstream f(“stu.txt”); ifstream f(“stu.txt”);
ofstream f1(“prn”); ofstream f1(“prn”);
char s[80]; char ch;
while(f.getline(s,80))) or while(f.get(ch)))
f1<<s<<endl; f1<<ch;
f.close( ); f.close( );
} }

Ans5. void clstudent( )


{
ifstream f(“stu.txt”);
char s[80];
int count=0;
while(f.getline(s,80)))
count++;
f.close( );
cout<<”\nThe number of lines : “<<count;
}

Ans6. void ccstudent( ) void ccstudent( )


{ {
ifstream f(“stu.txt”); ifstream f(“stu.txt”);
char ch; f.seekg(0,ios::end);
int count=0; int count=f.tellg( )/sizeof(ch);
while(f.get(ch))) f.close( );
count++; or cout<<”\nThe no. of chars : “<<count;
f.close( ); }
cout<<”\nThe no. of chars : “<<count;
}

Ans7. #include <ctype.h>


void cdstudent( )
{
ifstream f(“stu.txt”);
char ch;
int count=0;
while(f.get(ch)))
{
if(isdigit(ch))
count++;
}
f.close( );
cout<<”\nThe no. of digits : “<<count;
}

Ans8. #include <ctype.h>


void ccapstudent( )
{
ifstream f(“stu.txt”);
char ch;
int count=0;
while(f.get(ch)))
{
if(isupper(ch))
count++;
}
f.close( );
cout<<”\nThe no. of capital letters : “<<count;
}

Ans9. void cwdstudent( )


{
ifstream f(“stu.txt”);
char w[20];
int count=0;
while(f>>w)
count++;
f.close( );
cout<<”\nThe no. of words : “<<count;
}

Ans10. void cvwstudent( )


{
ifstream f(“stu.txt”);
char ch;
int count=0;
while(f.get(ch)))
{
if(ch==’a’ || ch==’e’ || ch==’i’ || ch==’o’ || ch==’u’)
count++;
}
f.close( );
cout<<”\nThe no. of vowels : “<<count;
}

Ans11. void clastudent( )


{
ifstream f(“stu.txt”);
char s[80];
int count=0;
while(f.getline(s,80)))
{
if(s[0]==’A’)
count++;
}
f.close( );
cout<<”\nThe lines starting with A is : “<<count;

Ans12. void cwvstudent( )


{
ifstream f(“stu.txt”);
char w[20];
int count=0;
while(f>>w)
{
if(w[0]==’a’ || w[0]==’e’ || w[0]==’i’ || w[0]==’o’ || w[0]==’u’)
count++;
}
f.close( );
cout<<”\nThe number of words starting with a vowel is : “<<count;
}

Ans13. #include <string.h>


void cisstudent( )
{
ifstream f(“stu.txt”);
char w[20];
int count=0;
while(f>>w)
{
if(strcmp(“is”,w)==0)
count++;
}
f.close( );
cout<<”\nThe number of occurrences of <is> are : “<<count;
}

Ans14. #include <ctype.h>


void chcasestudent( )
{
fstream f;
f.open(“stu.txt”,ios::in | ios::out);
char ch;
while(f.get(ch))
{
if(isupper(ch))
ch=tolower(ch);
else if(islower(ch))
ch=toupper(ch);
f.seekg(-sizeof(ch),ios::cur);
f.put(ch);
}
f.close( );
}

Ans15. #include <ctype.h>


void copystudent( )
{
ifstream f(“stu.txt”);
ofstream f1(“copy.txt”);
char w[20];
while(f>>w)
{
if(isupper(w[0]))
f1<<w<<’ ‘;
}
f.close( );
f1.close();
}

Ans16. #include <ctype.h>


#include <stdio.h>
void delstudent( )
{
ifstream f(“stu.txt”);
ofstream f1(“temp.txt”);
char w[20];
while(f>>w)
{
if(!(islower(w[0])))
f1<<w<<’ ‘;
}
f.close( );
f1.close();
remove(“stu.txt”);
rename(“temp.txt”,”stu.txt”);
}

Ans17. #include <string.h>


void serstudent(char name[20] )
{
ifstream f(“stu.txt”);
char w[20];
int flag=0;
while(f>>w)
{
if(strcmp(w,name)==0)
{
flag=1;
break;
}
}
f.close( );
if(flag==1)
cout<<”\nSuccessful”;
else
cout<<”\nUnsuccessful”;
}

You might also like