Problem statement
I would like to append a string " are awesome !", to another string "Ice creams" read in from stdin using getline function.
Thus producing "Ice creams are awesome !"
If the input is typed manually, the append operation works flawlessly.
However, if the input is taken from a file through redirection command "<" the append operation does not behave similarly.
Thus producing are awesome ! Ice creams.
Code
Let me demonstrate the problem through code.
#include<iostream>
int main()
{
// err : will be read from stdin using getline
// err : is expected to fail in appending
std::string err;
// ok : will be modified internally
// ok : is expected to succeed in appending
std::string ok = "Pancakes";
std::cout<<"Before executing std::getline..."<<std::endl;
std::cout<<"err \t:\t"<<err<<std::endl;
std::cout<<"ok \t:\t"<<ok<<std::endl;
std::cout<<"-------------------"<<std::endl;
// we now use getline to read in string value for err
// stdin can have any string , let's assume it is "Ice creams"
std::cout<<"-------------------"<<std::endl;
std::cout<<"Please enter string manually or < from file "<<std::endl;
std::getline(std::cin,err);
std::cout<<"-------------------"<<std::endl;
std::cout<<"After executing std::getline..."<<std::endl;
std::cout<<"err \t:\t"<<err<<std::endl;
std::cout<<"ok \t:\t"<<ok<<std::endl;
std::cout<<"-------------------"<<std::endl;
//-------------------------------------------//
// THE PROBLEM //
//-------------------------------------------//
// we try to append to err
err += " are awesome !";
// we try to append to ok
ok += " are awesome !";
std::cout<<"After executing append operation..."<<std::endl;
std::cout<<"Where,the '+=' operator is used..."<<std::endl;
std::cout<<"err \t:\t"<<err<<std::endl;
std::cout<<"ok \t:\t"<<ok<<std::endl;
std::cout<<"-------------------"<<std::endl;
return 0;
}
Terminal output
The compilation is done using g++ running in a Windows 10 machine with the help of WSL functionality.
C:\test>wsl g++ test.cpp -o a.out -std=c++11
C:\test>wsl ./a.out
Before executing std::getline...
err :
ok : Pancakes
-------------------
-------------------
Please enter string manually or < from file
Ice creams
-------------------
After executing std::getline...
err : Ice creams
ok : Pancakes
-------------------
After executing append operation...
Where,the '+=' operator is used...
err : Ice creams are awesome !
ok : Pancakes are awesome !
-------------------
C:\test>wsl ./a.out < in-test.in
Before executing std::getline...
err :
ok : Pancakes
-------------------
-------------------
Please enter string manually or < from file
-------------------
After executing std::getline...
err : Ice creams
ok : Pancakes
-------------------
After executing append operation...
Where,the '+=' operator is used...
are awesome ! Ice creams
ok : Pancakes are awesome !
-------------------
C:\test>
Results
- Append produces correct results for manual input from stdin.
- Append produces garbled results for input from file in-test.in from stdin.
Note : in-test.in file contains one line with the string "Ice creams".
Note : If it is difficult to notice the error, then let me point you to it.
It is the line are awesome ! Ice creams located at the tail of the Terminal output.
Questions
- Why does this happen?
- How to avoid it?