Reading a binary file like a matrix of ints

This is a discussion on Reading a binary file like a matrix of ints within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I would like to read a binary file as quickly as possible. Right now I am reading integer ...

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

    Reading a binary file like a matrix of ints

    Hi everyone,

    I would like to read a binary file as quickly as possible. Right now I am reading integer by integer, but that could end up being slow.. I imagine, it is probably best to put it all in a big buffer, but then I don't know how to convert that to an array of integers.

    so far this is a snippet of the code I have:


    Code:
        
    std::ifstream file("InvDat.dat",
            ios_base::in | ios_base::binary);
    
        char buf[BUF_SIZE]; //BUF_SIZE is 4
    
        if(file.is_open())
        {
    		while (file.read(reinterpret_cast<char*>(&buf[0]), sizeof(uint32_t)))
    		{
    			uint32_t *num = reinterpret_cast<uint32_t* >(&buf[0]);
    	        std::cout << left << setw(10) << *num << " ";
    		}
    		std::cout << endl;
        }
        file.close();
    Any help appreciated,

    Ted.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well you've got num, right? That's your array. num[0] is the first number, num[1] is the second number, etc.

    The only difficulty is that four bytes may or may not even get you one number, depending on your system. You should make your buffer as big as you need it.

    There's also no good reason for you to make your buffer of type char.
    Code:
    int big_pile_of_numbers[1000];
    file.read(reinterpret_cast<char*>big_pile_of_numbers, sizeof(big_pile_of_numbers));

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

    what if the buffer isn't completely full?

    How could I tell that the buffer is completely full or more like when it isn't completely full as there were no 1000 numbers, how can I prevent myself from accessing garbage locations?

    Ted.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    From cplusplus.com:
    Calling member gcount after this function the total number of characters read can be obtained.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  2. Reading from binary file; fread problems?
    By pmgeahan in forum C Programming
    Replies: 3
    Last Post: 01-15-2009, 05:07 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM

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