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

Click For Summary

Discussion Overview

The discussion revolves around how to read specific lines from an input file in C++, particularly in the context of a class assignment involving account information. Participants explore various methods for locating and processing account data based on user input.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes the assignment involving reading account data from a file and expresses difficulty in retrieving a specific line based on user input.
  • Another participant suggests a real-life analogy of searching through papers to find a specific account, questioning whether to read all data first or to search line-by-line as data is read.
  • A different participant outlines a step-by-step method for accessing a specific account by reading lines sequentially and parsing them until the target account is found.
  • One participant notes that modern operating systems do not allow skipping directly to a specific line in a file, emphasizing the need to read through the file from the beginning.
  • Additional considerations are raised regarding record size, whether the file is sorted, and the efficiency of reading all records into memory versus searching sequentially.

Areas of Agreement / Disagreement

Participants present multiple approaches and considerations, indicating that there is no consensus on the best method to retrieve specific lines from a file. Various opinions on efficiency and practicality are shared, reflecting differing views on how to handle the problem.

Contextual Notes

Participants mention limitations related to file structure, such as variable versus fixed record sizes, and the implications of sorted versus unsorted data on search strategies. These factors contribute to the complexity of the problem without resolving it.

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.
 

Similar threads

Replies
81
Views
8K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
12
Views
10K
Replies
3
Views
2K