Christopher Alvear
Christopher Alvear

Reputation: 11

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?

Upvotes: 1

Views: 43

Answers (1)

Lester Cheung
Lester Cheung

Reputation: 2030

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 ^_^)

Upvotes: 1

Related Questions