简体   繁体   English

getline()不读取第一行

[英]getline() not reading first lines

I am c++ beginner and this is for school.. 我是c ++初学者,这是学校。

I am trying to read a file about 28kb big. 我正在尝试读取一个大约28kb的文件。 The program works but it doesnt print the first 41 lines. 该程序可以运行,但是不会打印前41行。 It works fine with a smaller file. 对于较小的文件,它可以正常工作。 At first i was reading into a char array and switch it to strings. 起初,我正在读入一个char数组并将其切换为字符串。 i also tried changing the log buffer but it apparently it should be big enough.. I feel like this should be very simple, but just cant figure it out.. 我也尝试过更改日志缓冲区,但显然它应该足够大..我觉得这应该很简单,但无法弄清楚。

Any help will be greatly apreciated.. Thanks! 任何帮助将不胜感激..谢谢!

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cerrno>

using namespace std;

struct espion
{
    char nom[30];
    char pays[20];
    char emploi[29];

};


int main()
{
    const int MAX_NOM = 30, MAX_PAYS = 20, MAX_EMPLOI = 29;
    char nomFichier[50] = "espion.txt";



    ifstream aLire;
    aLire.open(nomFichier, ios::in|ios::binary);

    if(!aLire.is_open()){
        exit(EXIT_FAILURE);
    }


    std::string infoEspion;


    while(aLire)
    {
        infoEspion.clear();
        std::getline(aLire, infoEspion);
        cout << infoEspion ;

    }

    aLire.close();


    system("pause");
    return 0;

}

From the system("pause"), it looks like you're running on Windows. 从系统(“暂停”)看,您似乎正在Windows上运行。 With ios::binary, the end-of-line marker is not translated, and the cout << infoEspion; 使用ios :: binary,行尾标记不会翻译,并且cout << infoEspion; statement prints these "raw" lines in such a way that all of the lines are written on top of each other. 语句以这样的方式打印这些“原始”行,即所有行都写在彼此的顶部。 (More specifically, each line will end with a return but no newline, so the cursor goes back to the start of the same line after executing each cout statement.) If you take out the ios::binary, you will echo all of the input on a single, very long line. (更具体地说,每行将以return结尾,但没有换行符,因此在执行每个cout语句后,光标将返回到同一行的开头。)如果取出ios :: binary,则将回显所有在一条很长的线上输入。 Changing the statement to cout << infoEspion << endl; 将语句更改为cout << infoEspion << endl; will echo all of the lines. 将回显所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM