E
Eric Lilja
Hello, I'm writing a simple program that upon start-up needs to open a
text file and read a value on the last line. Then all other accesses to
the file will be writes (at the end of it). I'm having two problems
with this...I tried opening the file for both writing and reading with:
std::fstream file("budget.txt", std::ios_base::in | std::ios_base:ut
| std::ios_base::app);
if(!file)
std::cerr << "Error opening file!" << std::endl;
This compiles but it fails to open the file. So I removed the
std::ios_base::in flag and now the file is opened correctly (and its
previous content left intact), but my code for reading doesn't seem to
work.
static double
obtain_balance(std::fstream& file)
{
std::string line;
while(std::getline(file, line))
std::cout << line << std::endl;
/* The last line should always contain the balance,
in the following form:
balance yyyy-mm-dd xxxxxxxx */
assert(line.substr(0, 7) == "balance");
std::string s = "balance yyyy-mm-dd ";
return convert<double, std::string>(line.substr(0, s.length()));
}
I need to read a value on the last line and the only way I could think
to get to the last line is to read the file line by line. But it
doesn't work because nothing is outputted so the assertion fails
because the line variable is empty. I guess this is because I didn't
specify the std::ios_base::in-flag, but when I did the file wouldn't
open as I explained above..
So do I need to first open the file for reading, obtain the balance,
then close it and open it for writing (with app) and what is the best
way to seek to the beginning of the last line when writing?
/ E
text file and read a value on the last line. Then all other accesses to
the file will be writes (at the end of it). I'm having two problems
with this...I tried opening the file for both writing and reading with:
std::fstream file("budget.txt", std::ios_base::in | std::ios_base:ut
| std::ios_base::app);
if(!file)
std::cerr << "Error opening file!" << std::endl;
This compiles but it fails to open the file. So I removed the
std::ios_base::in flag and now the file is opened correctly (and its
previous content left intact), but my code for reading doesn't seem to
work.
static double
obtain_balance(std::fstream& file)
{
std::string line;
while(std::getline(file, line))
std::cout << line << std::endl;
/* The last line should always contain the balance,
in the following form:
balance yyyy-mm-dd xxxxxxxx */
assert(line.substr(0, 7) == "balance");
std::string s = "balance yyyy-mm-dd ";
return convert<double, std::string>(line.substr(0, s.length()));
}
I need to read a value on the last line and the only way I could think
to get to the last line is to read the file line by line. But it
doesn't work because nothing is outputted so the assertion fails
because the line variable is empty. I guess this is because I didn't
specify the std::ios_base::in-flag, but when I did the file wouldn't
open as I explained above..
So do I need to first open the file for reading, obtain the balance,
then close it and open it for writing (with app) and what is the best
way to seek to the beginning of the last line when writing?
/ E