Register to reply

C++ How do I call a specific line from an input file?

by jimmyv12
Tags: file, input, line, specific
Share this thread:
Feb21-09, 02:57 PM
P: 11
Alright, so my current class assignment is to create a program that will read several accounts, whether or not its a savings, the amount in the acocunt, then make calculations for interest and fines based on that info. The setup for the input file is like this:

account checking/savings amount
845756 C 1445
658697 S 157899

just an example not the real thing. Anyway, the only thing I cant figure out is how to call the specific line of the account(the user will input the account number). help please! Also, I'm in the first C++ class, so nothing too extreme.
Phys.Org News Partner Science news on
Security CTO to detail Android Fake ID flaw at Black Hat
Huge waves measured for first time in Arctic Ocean
Mysterious molecules in space
Feb21-09, 03:33 PM
Sci Advisor
PF Gold
Hurkyl's Avatar
P: 16,092
Quote Quote by jimmyv12 View Post
Anyway, the only thing I cant figure out is how to call the specific line of the account(the user will input the account number). help please! Also, I'm in the first C++ class, so nothing too extreme.
How would you do it in real life? e.g. if you had to find a particular sheet of paper in a big stack of papers, how would you go about doing it?
Feb22-09, 01:21 PM
P: 11
just flip through the papers until I find what I want? Haha I dont know!!

Would I input everything, then use my if statements to find the line? Or just use the if statement from the get-go to find the line in the document, and only input that line into the rest of my code?

Feb22-09, 02:44 PM
P: 287
C++ How do I call a specific line from an input file?

I'm not sure I understand the problem.

Say the file is:

1000 C $2000
2000 C $3000
1500 S $10000
3000 S $7500

Say you want to access account 1500. All you have to do is the following:

0. Open the file so that you're ready to read the first line.
1. Read a new line of input if one is available, otherwise goto step 4.
2. Parse the input so that you have an ordered triple (account, type, amount).
3. If account equals target, then goto step 5. Otherwise, throw out this line and the ordered triple and go back to step 1.
4. Couldn't find the account... error, and end the program or whatever.
5. Found the line, and you have the ordered triple (account, type, amount) for it... so do whatever you need to do.

In code, this could look like...

open file.

while(there is another line in the file) do
read the line.
parse the line into (account, type, amount).
if account equals target then
close file
return (account, type, amount).

close file.
return null.

You certainly could read the entire file into memory, parse it, and maintain an array of accounts such as...

Account accounts[...]

Where an Account is an ordered triple (account, type, amount), and then instead of opening and closing files and parsing, you can just search for the account in the array. It works the same as the above code, minus the opening, closing, and and reading from file. Also, no parsing... just check the array elements.
Feb22-09, 03:04 PM
Sci Advisor
HW Helper
P: 8,954
In general you can't skip to a particular line in a file on most modern OS/filesystems.
They don't store files as records they just store a stream of bytes, including the end of lines. You can skip to a particular byte - but not to a particular record.
You have to start at the beginning and read bytes until you counted the correct number of end-of-lines, you could then store the byte positions of each new line as an inex if you wanted.
Feb22-09, 03:40 PM
P: 366
A few additional considerations:
1. record size
All of the above apply perfectly to an ascii (text) file with variable record size.
In the case of a fixed record size (not specified in the problem statement), it is possible to jump around in the file using byte calculation techniques available in different languages including C, C++ and fortran.
2. file sorted or not
It is useless to jump around the file if it is not sorted. It is however, beneficial if the records are sorted according to a given criterion, in which case binary search can possibly be applied to advantage.
3. Order of magnitude of the toal number of recoords and the number of records required
Reading all the records into memory for subsequent processing is desirable if the number of records is small (up to say 10000), depending on the size of memory available and language used. Above that, it may perhaps be more desirable to read and reject what is not required until the required record is found

As suggeested by Hurkyl, it is much the same strategy as a human searching a list, sequentially if the list is short or not sorted, but by trial and error for a sorted list, as in a phone book.

Register to reply

Related Discussions
C program to display the file given filename as input Programming & Computer Science 2
Sample input file for PARCS code Nuclear Engineering 0
How could I script A code to synchronise A Specific File On My Pen Drive? Programming & Computer Science 1
How do I find the input Z of a cascaded transmission line? Electrical Engineering 2
Tangent line to a particles path at a specific time? Introductory Physics Homework 3