473,549 Members | 3,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.tx t", 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::getl ine(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.sub str(0, 7) == "balance");

std::string s = "balance yyyy-mm-dd ";
return convert<double, std::string>(li ne.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 3807
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.tx t", 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::getl ine(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.sub str(0, 7) == "balance");

std::string s = "balance yyyy-mm-dd ";
return convert<double, std::string>(li ne.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.tx t", 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2556
by: Shu-Hsien Sheu | last post by:
Hi, Does the seek method would close the file object after using a for loop? My program looks like this: f = open('somefile', 'r') for lines in f: some operations f.seek(0)
3
4592
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with "file.tell() and file.seek() in order to navigate through a file in order to print specific lines (for example, every 5th line). Allow me to...
1
4206
by: Waitman Gobble | last post by:
Hello, I am new to Python. I am having trouble with zipfile.py. On a Linux machine with python 2.4.2 I have trouble opening a zipfile. Python is complaining about the bit where it does a seek(-22,2). Looks to me like zipfile.py is trying to come back 22 bytes from the end of file. # python
2
12571
by: Harry J. Smith | last post by:
In the code below the two lines marked with ????????????????? do not work properly. The line: inCF.BaseStream.Seek(0, SeekOrigin.Begin); // rewind inCF to beginning of the file ?????????????????????? does not change the character position inCF.charPos, and the line:
4
2444
by: Felix Finch | last post by:
I have a perl test program which has about 80 test cases, each of which creates its own schema so I can remove them with DROP SCHEMA xxx CASCADE. Normally each test case creates and drops the same schema, but it can run a mode to preserve each schema and all the disk files for each test. I recently changed my cleanup code to run psql with...
0
1029
by: Art Chadbourne | last post by:
Here is the scenario. Access XP using VBA. I have a text field. The text that I have in the field is m-1234. I am running a >= seek. My first seek is m and it returns m-1234. My second seek is m- and it does not find it. My third seek is m-12 and it does not find it. My last seek is m-1234 and it finds it.
1
1761
by: Art Chadbourne | last post by:
Here is the scenario. Access XP using VBA. I have a text field. The text that I have in the field is m-1234. I am running a >= seek. My first seek is m and it returns m-1234. My second seek is m- and it does not find it. My third seek is m-12 and it does not find it. My last seek is m-1234 and it finds it.
59
7450
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when the recordset opens a table. When I write Set rst = db.OpenRecordset("MyTable",dbOpenTable, dbReadOnly) I get an error. I believe it's...
6
1772
by: DataSmash | last post by:
Hi group, I have a text file that contains thousands of lines and each line is 256 characters long. This is my task: For each line in the file, move to the 25th character, if the character is a "T", move to the 35th character of the line and read 5 characters from there. Capture these 5 characters and write them to a new text file, each 5
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7718
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6041
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.