Read from file. : Text File « File « C++






Read from file.

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; 
}


           
         
  








Related examples in the same category

1.Use the getline function with the C++ string classUse the getline function with the C++ string class
2.Write strings to disk
3.Write string to a file
4.Write to file: ofstream
5.Reading from a File
6.Writing to a File
7.reading a text file
8.writing on a text file
9.Sequential Files