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

In summary, the programmer is trying to figure out how to read a line from an input file and determine whether it is the target account. If it is, they go to step 5 and otherwise they go to step 1.
  • #1
jimmyv12
10
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
  • #2
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?
 
  • #3
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?
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 

1. How do I open an input file in C++?

To open an input file in C++, you can use the ifstream class and its open() function. First, include the fstream header file in your program. Then, create an ifstream object and use the open() function to specify the name of the file you want to open.

2. How do I read specific lines from an input file in C++?

To read specific lines from an input file in C++, you can use the getline() function. This function takes two parameters: the ifstream object you created to open the file, and a string variable to store the line that is read. You can use a loop to iterate through the lines of the file and use a counter variable to specify which line you want to read.

3. How do I extract specific data from a line in C++?

To extract specific data from a line in C++, you can use the substr() function. This function takes two parameters: the starting index of the substring and the length of the substring you want to extract. You can use this function in conjunction with the getline() function to extract specific data from a line in the input file.

4. How do I handle errors when reading from an input file in C++?

To handle errors when reading from an input file in C++, you can use the fail() function. This function checks if there was an error while reading from the file and returns a boolean value. You can use this function in an if statement to handle any errors that may occur while reading from the input file.

5. How do I close an input file in C++?

To close an input file in C++, you can use the close() function. This function takes no parameters and simply closes the file that was previously opened using the open() function. It is good practice to always close files after you are finished using them to free up resources and avoid any potential errors.

Similar threads

  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
3
Views
880
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
Back
Top