469,591 Members | 1,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

Seek to the last line in a text file

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::out
| 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

Nov 30 '05 #1
2 3549
Eric Lilja wrote:
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::out
| std::ios_base::app);
You should use std::ios_base::ate instead of 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?


Since the last line has a known format and length, just use
istream::seekg() to move the get pointer back to the proper location
from the end. (Note: the put pointer is manipulated independently from
the get pointer.) For an example, see:

http://www.cplusplus.com/ref/iostrea...eam/seekg.html

Cheers! --M

Nov 30 '05 #2
Eric Lilja wrote:

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::out
| std::ios_base::app);


Have you read the documentation what std::ios_base::app does?

http://www.roguewave.com/support/doc...ibug/30-3.html

--
Karl Heinz Buchegger
kb******@gascad.at
Dec 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Shu-Hsien Sheu | last post: by
3 posts views Thread by Pernell Williams | last post: by
1 post views Thread by Waitman Gobble | last post: by
2 posts views Thread by Harry J. Smith | last post: by
4 posts views Thread by Felix Finch | last post: by
reply views Thread by Art Chadbourne | last post: by
1 post views Thread by Art Chadbourne | last post: by
6 posts views Thread by DataSmash | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.