Reputation: 13
Ok here's the problem and I'm despaired about it. I get a 9*9 table using
for (int i=0 ; i < 9 ; i++)
for (int j=0 ; j < 9 ; j++)
cin >> table [i][j];
I want to make some changes to this table . so I get a command using:
getline (cin , command);
so user would type a command and after parsing it I will make the changes to the table. (it has to be getline() because command is an string and contains spaces)
I have an in.txt containing the table . I give this file to my code using :
g++ main.cpp
./a.out < in.txt .
(these two commands are written in terminal)
after that I want to give my command using keyboard . but the getline() doesn't get anything from keyboard . I tried using
cin.ignore() , cin.clear() , cin.sync () , even cin.get()
but it always passes the getline() .
Any suggestions would be appreciated . thanks in advance.
Upvotes: 0
Views: 825
Reputation: 16090
Well this is obvious why any input from standard input won't work. This is not limited to getline
. operator>>
also won't work, nor anything associated with std.cin
or stdin
.
This is because ./a.out < in.txt
is not "giving a txt to a program". It is redirecting the standard input. By default it is a keyboard input from terminal, but you are switching it to a file on a shell level.
If you wanted to switch from terminal associated input to a file, and then backwards, this would be handy: How to redirect cin and cout to files?. Though, in this your case, you will not have the original buffer associated with the terminal, precisely because you make OS not give it to you. You would need to obtain somehow from the system. This would be more complicated (well it would be os specific and less portable) than below suggestions.
I would suggest you to pass the filename as an argument and do not redirect standard input. Or, if you are gonna write some command prompt anyways, make a load_file
command, for loading the file. Then load the file using regular means designed for files e.g. ifstream
.
Upvotes: 1