C++/File/Text File

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Read from file.

 
 
#include <iostream> 
#include <fstream> 
using namespace std; 
 
int main() 
{ 
  ofstream out("test"); 
  if(!out) { 
    cout << "Cannot open file.\n"; 
    return 1; 
   } 
 
  out << 10 << " " << 123.23 << endl; 
  out << "This is a short text file."; 
 
  out.close(); 

  char ch; 
  int i; 
  float f; 
  char str[80]; 
 
  ifstream in("test"); 
  if(!in) { 
    cout << "Cannot open file.\n"; 
    return 1; 
  } 
 
  in >> i; 
  in >> f; 
  in >> ch; 
  in >> str; 
 
  cout << i << " " << f << " " << ch << endl; 
  cout << str; 
 
  in.close(); 
  return 0; 
}


reading a text file

  
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }else 
    cout << "Unable to open file"; 
  return 0;
}


Reading from a File

  
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
   char data[80];
   ofstream outfile;
   outfile.open("students.dat");
   cout << "Writing to the file" << endl;
   cout << "Enter class name: "; 
   cin.getline(data, 80);
   outfile << data << endl;
   cout << "Enter number of students: "; 
   cin >> data;
   cin.ignore();
   outfile << data << endl;
   outfile.close();
   ifstream infile; 
   cout << "Reading from the file" << endl; 
   infile.open("students.dat"); 
   infile >> data; 
   cout << data << endl; 
   infile >> data; 
   cout << data << endl; 
   infile.close();
   return 0;
}


Sequential Files

  
#include <fstream>
#include <iostream>
using namespace std;
int main() 
{
   char buffer[256];
   fstream myfile;
   myfile.open("test2.txt",ios::out | ios::trunc);
   if (myfile.is_open())
   {
        myfile << "This outputting a line.\n";
        myfile.close();
   }
   myfile.open("test.txt",ios::in);
   myfile.getline(buffer,100);
   cout << "The file contains   " << buffer << "\n";
   myfile.close();
   myfile.open("test.txt",ios::app);
   myfile << " Hey this is another line \n";
   myfile.close();
   myfile.open("test.txt",ios::in);
   myfile.getline(buffer,200);
   cout << "The file contains   " << buffer << "\n";
   myfile.close();
   return 0;
}


Use the getline function with the C++ string class

 
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main ()
{
   string data;
   ofstream outfile;
   outfile.open("file.dat");
   cout << "Writing to the file" << endl;
   cout << "Enter class name: "; 
   getline(cin, data); 
   outfile << data<< endl;
   cout << "Enter your id: "; 
   cin >> data;
   cin.ignore();
   outfile << data<< endl;
   outfile.close();
   ifstream infile; 
   cout << "Reading from the file" << endl; 
   infile.open("file.dat"); 
   getline(infile, data);
   cout << data << endl; 
   getline(infile, data);
   cout << data << endl; 
   infile.close();
   return 0;
}


Write strings to disk

 
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
  if(argc!=2) {
    cout << "Usage: WRITE <filename>\n";
    return 1;
  }
  ofstream out(argv[1]); // output file
  if(!out) {
    cout << "Cannot open output file.\n";
    return 1;
  }
  char str[80];
  cout << "Write strings to disk, "$" to stop\n";
  do {
    cout << ": ";
    cin >> str;
    out << str << endl;
  } while (*str != "$");
  out.close();
  return 0;
}


Write string to a file

 
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
  ofstream out("test", ios::out | ios::binary);
  if(!out) {
    cout << "Cannot open output file.\n";
    return 1;
  }
  double num = 100.45;
  char str[] = "www.java2s.com";
  out.write((char *) &num, sizeof(double));
  out.write(str, strlen(str));
  out.close();
  return 0;
}


Write to file: ofstream

 
 
#include <iostream> 
#include <fstream> 
using namespace std; 
 
int main() 
{ 
  ofstream out("test"); 
  if(!out) { 
    cout << "Cannot open file.\n"; 
    return 1; 
   } 
 
  out << 10 << " " << 123.23 << endl; 
  out << "This is a short text file."; 
 
  out.close(); 
 
  return 0; 
}


writing on a text file

  
#include <iostream>
#include <fstream>
using namespace std;
int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open()){
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else 
    cout << "Unable to open file";
  return 0;
}


Writing to a File

  
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
   char data[80];
   ofstream outfile;
   outfile.open("students.dat");
   cout << "Writing to the file" << endl;
   cout << "Enter class name: "; 
   cin.getline(data, 80);
   outfile << data << endl;
   cout << "Enter number of students: "; 
   cin >> data;
   cin.ignore();
   outfile << data << endl;
   outfile.close();
   return 0;
}