SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
www.cppforschool.com
Basic Operation On Text File In C++
File I/O is a five-step process:
1. Include the header file fstream in the program.
2. Declare file stream object.
3. Open the file with the file stream object.
4. Use the file stream object with >>, <<, or other input/output functions.
5. Close the files.
Following program shows how the steps might appear in program.
Program to write in a text file
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
fout.open("out.txt");
char str[300] = "Time is a great teacher but
unfortunately it kills all its pupils. Berlioz";
//Write string to the file.
fout << str;
fout.close();
return 0;
}
Program to read from text file and display it
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream fin;
fin.open("out.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
cout << ch;
}
fin.close();
return 0;
}
Program to count number of characters.
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream fin;
fin.open("out.txt");
int count = 0;
char ch;
while(!fin.eof())
{
fin.get(ch);
count++;
}
cout << "Number of characters in file are " << count;
fin.close();
return 0;
}
Program to count number of words
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream fin;
fin.open("out.txt");
int count = 0;
char word[30];
while(!fin.eof())
{
fin >> word;
count++;
}
cout << "Number of words in file are " << count;
fin.close();
return 0;
}
Program to count number of lines
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream fin;
fin.open("out.txt");
int count = 0;
char str[80];
while(!fin.eof())
{
fin.getline(str,80);
count++;
}
cout << "Number of lines in file are " << count;
fin.close();
return 0;
}
Program to copy contents of file to another file.
#include<fstream>
using namespace std;
int main()
{
ifstream fin;
fin.open("out.txt");
ofstream fout;
fout.open("sample.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
fout << ch;
}
fin.close();
return 0;
}
Basic Operation on Binary File In C++
When data is stored in a file in the binary format, reading and writing
data is faster because no time is lost in converting the data from one format to
another format. Such files are called binary files. This following program
explains how to create binary files and also how to read, write, search, delete
and modify data from binary files.
#include<iostream>
#include<fstream>
#include<cstdio>
using namespace std;
class Student
{
int admno;
char name[50];
public:
void setData()
{
cout << "nEnter admission no. ";
cin >> admno;
cout << "Enter name of student ";
cin.getline(name,50);
}
void showData()
{
cout << "nAdmission no. : " << admno;
cout << "nStudent Name : " << name;
}
int retAdmno()
{
return admno;
}
};
/*
* function to write in a binary file.
*/
void write_record()
{
ofstream outFile;
outFile.open("student.dat", ios::binary | ios::app);
Student obj;
obj.setData();
outFile.write((char*)&obj, sizeof(obj));
outFile.close();
}
/*
* function to display records of file
*/
void display()
{
ifstream inFile;
inFile.open("student.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{
obj.showData();
}
inFile.close();
}
/*
* function to search and display from binary file
*/
void search(int n)
{
ifstream inFile;
inFile.open("student.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() == n)
{
obj.showData();
}
}
inFile.close();
}
/*
* function to delete a record
*/
void delete_record(int n)
{
Student obj;
ifstream inFile;
inFile.open("student.dat", ios::binary);
ofstream outFile;
outFile.open("temp.dat", ios::out | ios::binary);
while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() != n)
{
outFile.write((char*)&obj, sizeof(obj));
}
}
inFile.close();
outFile.close();
remove("student.dat");
rename("temp.dat", "student.dat");
}
/*
* function to modify a record
*/
void modify_record(int n)
{
fstream file;
file.open("student.dat",ios::in | ios::out);
Student obj;
while(file.read((char*)&obj,sizeof(obj)))
{
if(obj.retAdmno() == n)
{
cout << "nEnter the new details of student";
obj.setData();
int pos = -1 * sizeof(obj);
file.seekp(pos, ios::cur);
file.write((char*)&obj, sizeof(obj));
}
}
file.close();
}
int main()
{
//Store 4 records in file
for(int i = 1; i <= 4; i++)
write_record();
//Display all records
cout << "nList of records";
display();
//Search record
cout << "nSearch result";
search(100);
//Delete record
delete_record(100);
cout << "nRecord Deleted";
//Modify record
cout << "nModify Record 101 ";
modify_record(101);
return 0;
}

Weitere ähnliche Inhalte

Was ist angesagt?

basics of file handling
basics of file handlingbasics of file handling
basics of file handling
pinkpreet_kaur
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
TAlha MAlik
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
Kumar
 

Was ist angesagt? (20)

Files in c++
Files in c++Files in c++
Files in c++
 
File handling
File handlingFile handling
File handling
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
source code which create file and write into it
source code which create file and write into itsource code which create file and write into it
source code which create file and write into it
 
Filehandling
FilehandlingFilehandling
Filehandling
 
[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream[Java] #7 - Input & Output Stream
[Java] #7 - Input & Output Stream
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
File handling
File handlingFile handling
File handling
 
Deletion of a Record from a File - K Karun
Deletion of a Record from a File - K KarunDeletion of a Record from a File - K Karun
Deletion of a Record from a File - K Karun
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
File Pointers
File PointersFile Pointers
File Pointers
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 

Ähnlich wie Chpater29 operation-on-file

File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptx
RutujaTandalwade
 
file handling final3333.pptx
file handling final3333.pptxfile handling final3333.pptx
file handling final3333.pptx
radhushri
 
Student Lab Activity CIS170 Week 6 Lab Instructions.docx
Student Lab Activity CIS170 Week 6 Lab Instructions.docxStudent Lab Activity CIS170 Week 6 Lab Instructions.docx
Student Lab Activity CIS170 Week 6 Lab Instructions.docx
florriezhamphrey3065
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
pinkpreet_kaur
 
Cis 170 c ilab 7 of 7 sequential files
Cis 170 c ilab 7 of 7 sequential filesCis 170 c ilab 7 of 7 sequential files
Cis 170 c ilab 7 of 7 sequential files
CIS321
 

Ähnlich wie Chpater29 operation-on-file (20)

File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
data file handling
data file handlingdata file handling
data file handling
 
File & Exception Handling in C++.pptx
File & Exception Handling in C++.pptxFile & Exception Handling in C++.pptx
File & Exception Handling in C++.pptx
 
file handling final3333.pptx
file handling final3333.pptxfile handling final3333.pptx
file handling final3333.pptx
 
Student Lab Activity CIS170 Week 6 Lab Instructions.docx
Student Lab Activity CIS170 Week 6 Lab Instructions.docxStudent Lab Activity CIS170 Week 6 Lab Instructions.docx
Student Lab Activity CIS170 Week 6 Lab Instructions.docx
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Introduction of file handling
Introduction of file handlingIntroduction of file handling
Introduction of file handling
 
working with files
working with filesworking with files
working with files
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
C++ppt.pptx
C++ppt.pptxC++ppt.pptx
C++ppt.pptx
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
 
File management in C++
File management in C++File management in C++
File management in C++
 
file management functions
file management functionsfile management functions
file management functions
 
Cis 170 c ilab 7 of 7 sequential files
Cis 170 c ilab 7 of 7 sequential filesCis 170 c ilab 7 of 7 sequential files
Cis 170 c ilab 7 of 7 sequential files
 

Mehr von Deepak Singh

Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
Deepak Singh
 
Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-i
Deepak Singh
 
Chapter24 operator-overloading
Chapter24 operator-overloadingChapter24 operator-overloading
Chapter24 operator-overloading
Deepak Singh
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
Deepak Singh
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
Deepak Singh
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-files
Deepak Singh
 
Chapter20 class-example-program
Chapter20 class-example-programChapter20 class-example-program
Chapter20 class-example-program
Deepak Singh
 
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructor
Deepak Singh
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
Deepak Singh
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
Deepak Singh
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
Deepak Singh
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
Deepak Singh
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
Deepak Singh
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
Deepak Singh
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 

Mehr von Deepak Singh (20)

Computer networks - CBSE New Syllabus (083) Class - XII
Computer networks - CBSE  New Syllabus (083) Class - XIIComputer networks - CBSE  New Syllabus (083) Class - XII
Computer networks - CBSE New Syllabus (083) Class - XII
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Chapter25 inheritance-i
Chapter25 inheritance-iChapter25 inheritance-i
Chapter25 inheritance-i
 
Chapter24 operator-overloading
Chapter24 operator-overloadingChapter24 operator-overloading
Chapter24 operator-overloading
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Chapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-filesChapter21 separate-header-and-implementation-files
Chapter21 separate-header-and-implementation-files
 
Chapter20 class-example-program
Chapter20 class-example-programChapter20 class-example-program
Chapter20 class-example-program
 
Chapter19 constructor-and-destructor
Chapter19 constructor-and-destructorChapter19 constructor-and-destructor
Chapter19 constructor-and-destructor
 
Chapter18 class-and-objects
Chapter18 class-and-objectsChapter18 class-and-objects
Chapter18 class-and-objects
 
Chapter17 oop
Chapter17 oopChapter17 oop
Chapter17 oop
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
 
Chapter13 two-dimensional-array
Chapter13 two-dimensional-arrayChapter13 two-dimensional-array
Chapter13 two-dimensional-array
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Chapter 10 Library Function
Chapter 10 Library FunctionChapter 10 Library Function
Chapter 10 Library Function
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 

Chpater29 operation-on-file

  • 1. www.cppforschool.com Basic Operation On Text File In C++ File I/O is a five-step process: 1. Include the header file fstream in the program. 2. Declare file stream object. 3. Open the file with the file stream object. 4. Use the file stream object with >>, <<, or other input/output functions. 5. Close the files. Following program shows how the steps might appear in program. Program to write in a text file #include <fstream> using namespace std; int main() { ofstream fout; fout.open("out.txt"); char str[300] = "Time is a great teacher but unfortunately it kills all its pupils. Berlioz"; //Write string to the file. fout << str; fout.close(); return 0; } Program to read from text file and display it #include<fstream> #include<iostream> using namespace std; int main() {
  • 2. ifstream fin; fin.open("out.txt"); char ch; while(!fin.eof()) { fin.get(ch); cout << ch; } fin.close(); return 0; } Program to count number of characters. #include<fstream> #include<iostream> using namespace std; int main() { ifstream fin; fin.open("out.txt"); int count = 0; char ch; while(!fin.eof()) { fin.get(ch); count++; } cout << "Number of characters in file are " << count; fin.close(); return 0; }
  • 3. Program to count number of words #include<fstream> #include<iostream> using namespace std; int main() { ifstream fin; fin.open("out.txt"); int count = 0; char word[30]; while(!fin.eof()) { fin >> word; count++; } cout << "Number of words in file are " << count; fin.close(); return 0; } Program to count number of lines #include<fstream> #include<iostream> using namespace std; int main() { ifstream fin; fin.open("out.txt"); int count = 0; char str[80]; while(!fin.eof()) { fin.getline(str,80); count++; } cout << "Number of lines in file are " << count;
  • 4. fin.close(); return 0; } Program to copy contents of file to another file. #include<fstream> using namespace std; int main() { ifstream fin; fin.open("out.txt"); ofstream fout; fout.open("sample.txt"); char ch; while(!fin.eof()) { fin.get(ch); fout << ch; } fin.close(); return 0; } Basic Operation on Binary File In C++ When data is stored in a file in the binary format, reading and writing data is faster because no time is lost in converting the data from one format to another format. Such files are called binary files. This following program explains how to create binary files and also how to read, write, search, delete and modify data from binary files. #include<iostream> #include<fstream>
  • 5. #include<cstdio> using namespace std; class Student { int admno; char name[50]; public: void setData() { cout << "nEnter admission no. "; cin >> admno; cout << "Enter name of student "; cin.getline(name,50); } void showData() { cout << "nAdmission no. : " << admno; cout << "nStudent Name : " << name; } int retAdmno() { return admno; } }; /* * function to write in a binary file. */ void write_record() { ofstream outFile; outFile.open("student.dat", ios::binary | ios::app); Student obj; obj.setData(); outFile.write((char*)&obj, sizeof(obj)); outFile.close(); } /* * function to display records of file */
  • 6. void display() { ifstream inFile; inFile.open("student.dat", ios::binary); Student obj; while(inFile.read((char*)&obj, sizeof(obj))) { obj.showData(); } inFile.close(); } /* * function to search and display from binary file */ void search(int n) { ifstream inFile; inFile.open("student.dat", ios::binary); Student obj; while(inFile.read((char*)&obj, sizeof(obj))) { if(obj.retAdmno() == n) { obj.showData(); } } inFile.close(); } /* * function to delete a record */ void delete_record(int n) { Student obj; ifstream inFile; inFile.open("student.dat", ios::binary); ofstream outFile;
  • 7. outFile.open("temp.dat", ios::out | ios::binary); while(inFile.read((char*)&obj, sizeof(obj))) { if(obj.retAdmno() != n) { outFile.write((char*)&obj, sizeof(obj)); } } inFile.close(); outFile.close(); remove("student.dat"); rename("temp.dat", "student.dat"); } /* * function to modify a record */ void modify_record(int n) { fstream file; file.open("student.dat",ios::in | ios::out); Student obj; while(file.read((char*)&obj,sizeof(obj))) { if(obj.retAdmno() == n) { cout << "nEnter the new details of student"; obj.setData(); int pos = -1 * sizeof(obj); file.seekp(pos, ios::cur); file.write((char*)&obj, sizeof(obj)); } } file.close(); } int main() { //Store 4 records in file for(int i = 1; i <= 4; i++)
  • 8. write_record(); //Display all records cout << "nList of records"; display(); //Search record cout << "nSearch result"; search(100); //Delete record delete_record(100); cout << "nRecord Deleted"; //Modify record cout << "nModify Record 101 "; modify_record(101); return 0; }