C++ Читання

2075 / C++ / Читання з файлу

 

Читати файл по рядках

#include <iostream>
#include <fstream>
#include <string>

string line;
ifstream file("1.txt");
if (file.is_open()) {
  while (getline(file, line))
  {
    cout << line << '\n';
  }
  file.close();
}
else cout << "Не можу відкрити файл";

 

Читати файл по буквах

char ch;
while (file.get(ch))
{
  cout << ch << endl;
}
  
 
  
Видалити

if ( remove("1.txt") != 0 ){
  cout<< "Error deleting 1.txt";
}
 
 
 
Як не закриваючи файл почати читати спочатку

file.clear();
file.seekg(0, file.beg);

 

Розбити по пробілах

int a = line.find(" ");
rez += line.substr(0, a) + line.substr(a + 1, line.length() - a - 1);

// int a2 = line.find(" ", a1 + 1);