![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Questions about reading binary file into unsigned int arrays.Hello,
I am trying to read a binary file into an 2D unsigned int array.But the C++ .read() file i/o operation seems to work on char arrays only.Any one can show me an efficient solution?Thank u in advance. Lixiang |
|||
|
#2
|
|||
|
|||
Re: Questions about reading binary file into unsigned int arrays.Is that the ifstream::read method?
Just cast the unsigned int* to a char* before passing it into the method. Also make sure that you convert the size for allocating the array. The following is the example from the link (above) changed to read an unsigned int array: CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: Questions about reading binary file into unsigned int arrays.Quote:
The fstream read and write functions take an argument that is a pointer to char. If you want to write an integer, then you can cast the address of the int as a pointer to char. If you want to read an integer, then you can cast the address of the int as a pointer to char. When investigating things like this, I always like to start with a small file whose contents are known. In other words, I will create a binary file with a couple of integers then read it back. Now, since it is possible that you don't know for sure how to create a small binary file with known contents, I'll show a program that does exactly that: CPP / C++ / C Code:
Output on my Linux system: Code:
The size of the file "test.bin" created by this program on my system is 8. See Footnote. If this program runs successfully, you can try the following read the integers from the file. I used a vector of ints. You can use an array if you are careful not to read more than the size of the array. CPP / C++ / C Code:
Output: Code:
Dave Footnote: Since I wrote two ints, I conclude that the size of an int is 4. Of course I could have just printed the size of an int by something like the following to find out what the size of an int is with my compiler: CPP / C++ / C Code:
Note that the C and C++ standards documents specifically do not guarantee that the size of an int is always going to be 4, but that seems to be the case for any reasonably current compiler that you are likely to run across for personal workstations these days. If you are using an ancient compiler that has 16-bit ints, then sizeof(int) will be equal to 2. If sizeof(int) is equal to 2, I strongly recommend that people upgrade their compilers to something more current. Not just because of the size of ints (although that might be reason enough), but because old compilers with non-standard features and functions sometimes encourage beginners to get into bad habits and the old compilers lack many standard features that beginners should be learning. |
|
#4
|
|||
|
|||
Re: Questions about reading binary file into unsigned int arrays.Quote:
I was with you up to there. I'm thinking that writing the char equivalents of the bytes of an int is not very illuminating. If I wanted to see the integer values in the array in your program after reading them in, I might do something like: CPP / C++ / C Code:
I particularly appreciate seeing how to determine the size of a file with seekg/tellg. It also shows why C++ programmers usually use vectors instead of arrays, since with vectors it's not necessary to know the size of the file before starting to read. This part (knowing the size) is even more problematic for "2D arrays" if they have to be allocated dynamically. Whether it's a 2D array or a vector of vectors, we would probably end up reading one int at a time, or maybe a row at a time. Regards, Dave |
|
#5
|
|||
|
|||
Re: Questions about reading binary file into unsigned int arrays.Quote:
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Power Calibration Error In Nero Fix (hopefully) | matt3678 | Computer Hardware Forum | 60 | 20-Aug-2009 06:04 |
| contents of .txt file into 2D array | anirudhroxrulz | C Programming Language | 4 | 10-Apr-2008 23:45 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| After execution - Error cannot locate /Skin File? | WSCH | C++ Forum | 1 | 05-Mar-2005 21:03 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 08:10 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The