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

AI Thread Summary
To call a specific line from an input file in C++, one can read the file line by line, parsing each line to extract account information until the desired account number is found. The process involves opening the file, reading each line, and checking if the account number matches the target. If found, the program can return the account details; if not, it should handle the error appropriately. An alternative approach is to read the entire file into memory and store the accounts in an array for quicker access, especially if the number of records is manageable. Overall, the method chosen depends on factors like file size and whether the records are sorted.
jimmyv12
Messages
10
Reaction score
0
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 can't 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.
 
Technology news on Phys.org
jimmyv12 said:
Anyway, the only thing I can't 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?
 
just flip through the papers until I find what I want? Haha I don't 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?
 
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...

findAccount(target)
begin
open file.

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

close file.
return null.
end.

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.
 
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top