C++ tutorial-file input ouput


C++ text and data files

So far we haven’t talked about writing words or sentences to a hard drive or reading from it. This part will guide you through those processes.


A text file is a file that stores words and sentences in plain text. You must include the fstream.h header file before you can write or read files. To write something to the file, we use ofstream and to read something from the file we use ifstream. In example below, we create a file called test.txt and write TestingTesting to it and then read read the text back to display it on the screen. After writing or reading the file, you can use close() method to close the file.


//Writing data to the text file

#include<iostream.h>
#include<fstream.h>
void main(){
  ofstream out("test.txt");
  out<<”TestingTesting”;//writing something to text file
  out.close();
}
//Reading data from the text file
#include<iostream.h>
#include<fstream.h>
void main(){
ifstream in("test.txt");
char *mytext;
in>>mytext;//Reading something from file
cout<<mytext;//print it to the screen
in.close();
}

Appending to text files

One you write something to the file, the old text stored in the file will be removed automatically. To append it, you must add ios::app when you open the file to write. Example:
ofstream out("test.txt",ios::app);
 

Writing and reading data files


A data file is a file that is usually used to store structures. In the example below, we create a data file called student.dat and write students’ records to it. To write a structure to it, you must use write() method of ofstream. This method requires two parameters. The first parameter is a pointer to the structure and the second parameter is the size of the structure to be written to the file. To read the data from the file, you can use read() method of ifstream that requires two parameters like write() method
struct student
{
   int stnumber;
   char *stname;
   char *stsex;
};

//Write data to data file
#include<iostream.h>
#include<fstream.h>
void main(){
  student stu;
  stu.stnumber=123;
  stu.stname=”Dara”;
  stu.stsex=”Male”;
  ofstream out(“student.dat”);
  out.write((char *)(&stu),sizeof(stu));//write stuents’records to the data file
  out.close();
  return 0;
}
//Reading data from data file
#include<iostream.h>
#include<fstream.h>
void main(){
  ifstream in(“student.dat”);
  student stu;
  ifstream in((char *)(&stu),sizeof(stu);//reading students’ records from the file
  cout<<stu.stnumber<<”  “<<stu.stname<<”  “<<stu.stsex;//printing the records on the screen
  in.close();
  return 0;
}


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.