简体   繁体   中英

unexpected getline behavior

Originally was trying to read data using char* but switched to string cause was getting behavior as if there was a missing null terminator. made the problem minimal below but still getting very weird output

int main(int argc, char * argv[]){


    // file one:  flightData
    std::ifstream inFile1(argv[1]);
    if (!inFile1.is_open())
    {
        std::cout << "Could not open the file 1." << std::endl;
        return -1;
    }
    // file two: flightPlans
    std::ifstream inFile2(argv[2]);
    if (!inFile2.is_open())
    {
        std::cout << "Could not open the file 2." << std::endl;
        return -1;
    }
    //File three: output
    std::ofstream outputfile(argv[3]);
    if (!outputfile.is_open())
    {
        std::cout << "Could not open the output file" << std::endl;
        return -1;
    }
    std::string buffer;


    getline(inFile1, buffer);
    std::cout<<buffer<<std::endl;
    while (getline(inFile1, buffer)) {
        std::cout<<buffer;
        std::cout<<"help";
    }

   // flightPlanner system(inFile1);
  //  system.printF();
//    system.planFlights(inFile2,outputfile);

    return 0;
}

output is

4
helpDallas|Austin|50|50help

which i'm pretty sure is incorrect, interestingly when i add endl to cout buffer it gives me output i would expect not really sure whats going on

inFile1

4
Dallas|Austin|50|50
Dallas|Austin|50|50
Dallas|Austin|50|50
Dallas|Austin|50|50

When i run in debugger i get the output i expect:

4

Dallas|Houston|50|50
helpDallas|Houston|50|50
helpDallas|Houston|50|50
helpDallas|Houston|50|50help

any idea what could be going on?

Do you need flushing your stdout?

std::cout << std::flush;

Any chance your shell ate your outputs? Try pipping the output to "cat -A":

./a.out | cat -A

(Drive by comment - I may not know what I'm talking about ^_^)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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