The problem is the following, you need to read a few lines from the file and output. Starting from the second, numbering. To do this, made the counter and check it. But that's not the point. getline() truncates the first character in the lines, starting with the second. I can not understand what the problem is.

Example: An input file contains:

 First Second Third 

Program text: #include "stdafx.h" #include // enable the input-output functions #include // enable the read-write functions in the #include file

 using namespace std; // объявляем пространство имен void Func(const char *filename) { int counter = 0; char input; string line; ifstream a_1(filename, ios::in); if (a_1.is_open()) { while (a_1 >> input) { getline(a_1, line); if (counter == 0) { cout << line << "\n"; } else { cout << counter << " " << line << "\n"; } counter += 1; } } } int main() { Func("data.txt"); return 0; } 

    1 answer 1

    Well, you read it in

     while(a_1 >> input) 

    Well, it’s surprising that in a line line without the character that is in the input ? ...

    Check this:

     while(getline(a_1, line)) { ... 

    Here is the full text so that there are no questions:

     #include <iostream> // подключаем функции ввода-вывода #include <fstream> // подключаем функции чтения-записи в файл #include <string> using namespace std; // объявляем пространство имен void Func(const char *filename) { int counter = 0; string line; ifstream a_1(filename, ios::in); if (a_1.is_open()) { while (getline(a_1, line)) { if (counter == 0) { cout << line << "\n"; } else { cout << counter << " " << line << "\n"; } counter += 1; } } } int main() { Func("data.txt"); return 0; } 

    Here is the data.txt

     First Second Third 

    Here is the conclusion

     First 1 Second 2 Third 

    now work out?

    • Thanks, but how then to check that it is not over? - user228605
    • See amended answer. - Harry
    • In this case, the first line is ignored completely. - user228605
    • Did you accidentally leave getline also in the body of the loop? :) Remove it from there, you have already read the line in the title! Everything works correctly for me. - Harry
    • data.txt in a column. - user228605 4:39