Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Feb 21, 2009 #1
    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.
  2. jcsd
  3. Feb 21, 2009 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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?
  4. Feb 22, 2009 #3
    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?
  5. Feb 22, 2009 #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...

    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.
  6. Feb 22, 2009 #5


    User Avatar
    Science Advisor
    Homework Helper

    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.
  7. Feb 22, 2009 #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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook