reading from beginning of file a second time

This is a discussion on reading from beginning of file a second time within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I am reading a binary file completely, but I would like to restart reading the file, by moving ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    52

    reading from beginning of file a second time

    Hi everyone,

    I am reading a binary file completely, but I would like to restart reading the file, by moving to the beginning of the file again. I have tried using file.seekg(0, ios::beg); but it doesn't seem to work.

    In a readfile function I read the file once, and it works.

    Code:
        vector<uint32_t> buf(numcols);
    
        if(file.is_open())
        {
            while (file.read(reinterpret_cast<char*>(&buf[0]), sizeof(uint32_t)*numcols))
            {
                for(size_t i = 0; i < numcols; ++i)
                {
                    freq[i][buf[i]]++;
                }
            }
        } 
    
        file.seekg(0, ios::beg);    // This supposedly brings the pointer to the beginning  of the file again.
    in another function I try:

    Code:
     while (file.read(reinterpret_cast<char*>(&buf[0]), sizeof(uint32_t)*numcols))
            {
                for(size_t i = 0; i < numcols; ++i)
                {
                    freq[i][buf[i]]++;
                }
            }
    but strange numbers indicate that it isn't actually reading the file from the beginning.

    Ted.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21