User Tools

Site Tools


c_-_c_files:file_read

C - C++ Files - File read

#include <iostream> 
#include <fstream> 
using namespace std; 
 
int main() 
{ 
  char ch; 
  int i; 
  float f; 
  char str[80]; 
 
  ofstream out("test.txt"); 
  if(!out) 
  { 
    cout << "Cannot open file.\n"; 
    return 1; 
  } 
 
  out << 10 << " " << 123.23 << "\n"; 
  out << "This is a text."; 
  out.close(); 
 
  ifstream in("test.txt"); 
  if(!in) 
  { 
    cout << "Cannot open file.\n"; 
    return 1; 
  } 
 
  in >> i; 
  in >> f; 
  in >> ch; 
  in >> str; 
 
  cout << i << " " << f << " " << ch << "\n"; 
  cout << str; 
 
  in.close(); 
 
  return 0; 
}
c_-_c_files/file_read.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki