edit: RESOLVED. Moved ins >> val
to precede the loop then put ins.fail()
as the condition. I'm not entirely sure why this helped but it did and I will gladly be moving forward.
Original post:
I'm having an issue with the code below. For some reason, it reads in file.txt
and comes back as good but it the istream is reading input as if the file is blank. For reference, I'm working in Xcode. I've tried playing around with the scheme but nothing seems to work. I just don't understand how it's successfully reading in the file (I run a check in some of the ...
code and it says the file was opened successfully) but yet isn't pulling in any of the input.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;
inFile.open("file.txt");
// There exists a class called Position that isn't important for
// this but just clarifying
Position pos;
pos.read(inFile);
return 0;
}
void Position::read(istream& ins) {
int val;
char discard;
while (!(ins >> val)) {
ins.clear();
ins >> discard;
}
}
I'm relatively new to this, only been coding for about 6 months now. I would greatly appreciate any explanations. Thanks folks!
edit: the intention of this code is to pull in input from the file in the form of ints. If the input is anything other than an int
, the loop will reset to good state and pull in the non-int as a char
. I don't care as much about the fact that the actual text of the file is seemingly disappearing. The code was provided primarily for a bit of context.
Apologies for my lack of familiarity with Stack!
edit2: If i run the loop like this:
while (!(ins >> val)) {
ins.clear();
ins >> discard;
cout << discard;
}
It goes into an infinite loop because there is no input from the file. Not exactly sure how I would show I/O for this since the problem is that there isn't any input and therefore no output. When I test, it just runs and runs and runs until I stop it. The actual .txt file is NOT empty but it is somehow being treated as if it is.
edit3: Adding a few more lines as an attempt at clarification of what exactly is happening.
Thanks again!
Here's a short example that shows:
I hope it clarifies things. The question was a bit difficult to answer directly, because you never explain what ins
is.
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using std::string;
using std::ifstream;
using std::ofstream;
using std::vector;
void append_to_file(string const& filename, string const& text)
{
// Open the file in append mode
ofstream file(filename, std::ios_base::app);
// Write the text to the file
file << text;
}
void write_to_file(string const& filename, string const& text)
{
// Clear the file and open it
ofstream file(filename);
file << text;
}
// Read all the lines in the file
vector<string> read_all_lines(string const& filename)
{
ifstream file(filename);
vector<string> lines;
while(not file.eof() && not file.fail()) {
// Get the line
std::string line;
std::getline(file, line);
// Add the line into the vector
lines.push_back(std::move(line));
}
return lines;
}
int main()
{
string filename = "test-file.txt";
// Clear the file and write
write_to_file(filename, "This is a sentence.\n");
// Append some additional text to the file:
append_to_file(filename, "This is some additional text");
// Read all the lines in the file we wrote:
vector<string> lines = read_all_lines(filename);
// Print the lines we read:
for(auto& line : lines) {
std::cout << line << '\n';
}
}