Thread: Reading file from the bottom up

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    33

    Reading file from the bottom up

    I have a file that goes like
    Code:
    ++++07/17/2016++++Memo: wdawd
    retire (Chase Savings): 20
    investment (Chase College): 70
    Chase total: 90
    savings (IQ Savings): 70
    spending(IQ Checking): 40
    IQ total: 110
    Total: 200
    ++++END++++
    but I was wondering how I can read the file from the end to the beginning and copy to string. Like start at end and go to total and IQ and so on

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Read the whole file into an array of strings, then index the array backwards.

    Or use ftell/fgets to read the whole file forward to get an array of file positions marking the start of each line.
    Then index that array backwards to allow you to fseek/fgets each line in turn, but in reverse order.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Guest
    Guest
    screwlu, are you familiar with std::getline? I would use that to read each line into a std::string and push it onto a std::vector<std::string>, storing all lines. Afterwards, you could iterate backwards over that vector to process your data.

    If you literally want to reverse the file, not just the line order, then you could read the whole file into a single std::string and use its reverse iterators.
    Last edited by Guest; 09-03-2016 at 02:24 AM.

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    33
    Quote Originally Posted by Salem View Post
    Read the whole file into an array of strings, then index the array backwards.

    Or use ftell/fgets to read the whole file forward to get an array of file positions marking the start of each line.
    Then index that array backwards to allow you to fseek/fgets each line in turn, but in reverse order.
    I don't fully understand the fseek part, can you show me an example code?
    Quote Originally Posted by Guest View Post
    screwlu, are you familiar with std::getline? I would use that to read each line into a std::string and push it onto a std::vector<std::string>, storing all lines. Afterwards, you could iterate backwards over that vector to process your data.

    If you literally want to reverse the file, not just the line order, then you could read the whole file into a single std::string and use its reverse iterators.
    does that use up a lot of ram?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > I don't fully understand the fseek part, can you show me an example code?

    To really get fseek you have to know about file positions. A file position is essentially a number that marks where you are. It is meaningful to say, for instance, that the beginning of the file is different from your position after reading 400 bytes. The function fseek is programmed to help you manipulate this number in a meaningful way. It will move the position by a certain offset from either the beginning, (SEEK_SET) where you are now, (SEEK_CUR) or the end of the file (SEEK_END).

    Salem was saying that you should save ftell() return values in an array. Three guesses what this call does with those values:
    Code:
    fseek(finput,lineIndex[n - 1],SEEK_SET);

    Last edited by whiteflags; 09-04-2016 at 08:05 PM.

  6. #6
    Guest
    Guest
    Quote Originally Posted by screwlu View Post
    does that use up a lot of ram?
    Depends on how big your data is and how you want to process it. Is memory use even a concern for you in this case? There are always tradeoffs to be made between 1) easily understandable code, 2) memory consumption, 3) speed, but unless you're short on 2), I would focus on 1) and then 3).

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You can read one buffer-full of data from the end of the file, process that, then read the next buffer-full from before that, etc.

    A sketch of the idea (without error handling and with many details still to work out) :
    Code:
    char buf[BUFSIZ];
    
    fseek(f, 0, SEEK_END);
    long pos = ftell(f);
    if (pos == 0) /* file is empty */ ;
    
    pos -= pos % BUFSIZ; // make pos a multiple of BUFSIZ
    
    while (pos >= 0) {
        fseek(f, pos, SEEK_SET);
        size_t cnt = fread(buf, 1, BUFSIZ, f);
    
        // process buf
    
        pos -= BUFSIZ;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bottom-up tutorials?
    By audinue in forum Tech Board
    Replies: 6
    Last Post: 08-10-2009, 01:28 PM
  2. Mergesort bottom-up
    By christopheva in forum C Programming
    Replies: 3
    Last Post: 04-26-2009, 11:56 AM
  3. Replies: 19
    Last Post: 09-14-2006, 10:36 AM
  4. List Box to bottom
    By Coder87C in forum Windows Programming
    Replies: 1
    Last Post: 03-02-2004, 02:15 AM

Tags for this Thread