You need to open the file and read it line by line, that is, take the first line to put it into a variable, do certain operations with it, then take the second one, etc.
2 answers
ifstream file(fileName);//создаем объект потока istream по имени file // который инициализируется именем fileName, //вызывается функция file.open(); string str; //переменная стринг для строки while(getline(file,str)) //getline(istream & is, string &s,char c='\n'),читает из потока is, в строку s пока { //не встретит символ c (без этого символа до новой строки) // возвращает свой объект istream, в условии проверяется состояние iostate флагa, значение этого флага будет ложным, если достигнет конца файла, или будет ошибка ввода или читаемого типа fncn(str); // вызываем нужною функцию для полученной строки } - And for what a minus? Uncomment, pliz. - VladD
|
Here is the code not taken out of context
#include <iostream> #include <string> // подключаем строки #include <fstream> // подключаем файлы using namespace std; // используем стандартное пространство имен int main(){ string s; // сюда будем класть считанные строки ifstream file("C:\\PriceList.dat"); // файл из которого читаем (для линукс путь будет выглядеть по другому) while(getline(file, s)){ // пока не достигнут конец файла класть очередную строку в переменную (s) cout << s << endl; // выводим на экран s += "+"; // что нибудь делаем со строкой например я добавляю плюсик в конце каждой строки cout << s << endl; // и снова вывожу на экран но уже модифицированную строку (без записи ее в файл) } file.close(); // обязательно закрываем файл что бы не повредить его return 0; } - Uh ... Are you sure about
eof? If I'm not mistaken,eofforistream's returns true not when the stream is at the end of the file, but when the stream is at the end of the file, and the previous read attempt failed because of this . - VladD - To be honest, I myself am not sure, as far as I understand, when reading the next line, the pointer shifts to the end of this line. Not a long time ago, but the example works fine without errors. but here cplusplus.com/forum/beginner/11304 also has a similar code specified - perfect
- Does it work on an empty file? - VladD
- Yes, in an empty file, the loop is executed once but nothing gets into the line - perfect
|