Reputation: 37
I am new to C++ and wondered of there is a way to use just standard iostream to read in an input file (from using debugging properties: < filename) or other then get control back to the console to input something else later with cin.
I separated the file read part into a different function but it seems when I specify in the project properties the command to grab the file contents line by line with getline() it skips over any cin commands I issue later.
I'm sure this could just be a setup issue or I am may need to break it off into another program in the project somehow? This is a console app but surely there is a way to do both in the same project?
I have read that you can't use both cin and getline together but how does one input a file then go ask for more info from the user in a C++ app using visual studio?
Separate program and functions for the file read
'int lineIter = 0;
cin.getline(rowData, arraySize); // Grab first row of data
while (!cin.eof()) {
// output each row of data to screen:
cout << rowData << endl;
}
///// Increment for next ROW
lineIter++;
cin.getline(rowData, arraySize);'
Then later how do I go back to being able to use cin or other to get input from user?
I tried many variations of below later:
'cin.clear();
cin.ignore(arraySize);
cin >> selectR[b];'
and other variations of getline() but none stop program execution and I can't get them to do anything except try to read the file again.
I am using VS 2019 Community Edition
Upvotes: 0
Views: 307
Reputation: 113
I don't really understand what you're trying to achieve here.
Currently, the way you read your external file is by changing std::cin
's "source" from the console to the given file ; this change is made outside your program, at the debugger's level. So you will not be able to change the source back to the console from the program itself. (You might be able to do from the debugger while it's running though).
If you want to read external files and still be able to use std::cin
normally, why not using std::fstream
s ?
On the other hand, if you absolutely have to pass the file to the program through std::cin
, you should keep it default and simply copy-paste your file in the console. Be careful though : since the file probably contains newlines, you will not be able to use those as end of input, so you'll have to design another way for the program to reckognize the end of the file (two consecutive empty lines for example).
Upvotes: 0