![]() |
|
#1
|
|||
|
|||
Reading and writing binary files in certain formatHi,
I am trying to write a program that reads a binary file (an image with a 512B header) and writes in into another binary file( an image file with a new header-1024B). The program is suppose to change the header of the input file. The parameters of the input file (old header format) is different from the parameters of the output file (new header format). Both formats have some parameters in common. I initialized the other parametes that are not in common between the two formats to a nonsense value( I chose -1 since it is not an accetable value for the parametes of the image header). then according to the order of the parameters in the new format, data is either being read from the old format or from the nonsense initialized values. Then the image data is beign read and written exactly as they are in the inputfile. The problem that I have now is that the output file is not stable. when i run the program on a file for different times, i get output files with different sizes, sometimes even the output file is generated but the size of it is 0. I hope the way I explained it make sense. Does anyone know what might be the problem? Thank you |
|||
|
#2
|
|||
|
|||
|
What are you reading and writing: bytes, or what? Are you reading and writing integers with binary files, or what?
Show some code; give us some clues. Dave |
|
#3
|
|||
|
|||
|
Sorry for not being clear.
I am reading and writing raw data. The input file is a raw data file output of a microscope. The file has a 512 bytes header that includes the information about the image like size, data, color, and etc. The output file again is a binary file. The output file should have a header of 1024 bytes plus the image bytes. so the program is reading the bytes from the input files and write them in different order in the output file (i.e. the program should change the header of the input file). But since the output file is 512 B larger than the input file, 512 bytes are being read from a class that includes the extra variables of the output file initialized to -1. All the bytes are being read as longs. CPP / C++ / C Code:
Last edited by Garth Farley : 04-Aug-2004 at 05:45.
Reason: Added C/C++ syntax highlighting
|
|
#4
|
|||
|
|||
|
I think I understand what you have in mind.
A couple of questions: this isn't right: CPP / C++ / C Code:
It shouldn't even compile since the first argument to inFile.read should be a pointer to a char, not a pointer to an unsigned short. In either case (imageIn.bpp == 8, or imagiIn.bpp == 16) you are trying to read 2*imageIn.totalx*imageIn.totaly bytes. Is this correct? Are you sure that you have read the .totalx and .totaly quantities correctly? (Use cout<< to show the values you are actually reading). Are you actually reading and writing that many bytes? Dave |
|
#5
|
|||
|
|||
|
in the case (bpp == 8 ) , the number of bits per pixel of the image is 8 and in the other case, there are 16 bits per pixel. so when there is one byte per pixel, i am reading it to a char array which is one byte per cell. When there is 2 bytes per pixel, i am reading it to a short array which is 2 bytes per pixel. so each array cell represents a pixel data of the image. I had another program before this that only read and write the image data of a raw data file. This part of the code is from that program. I do not ger compiler error though. If i change the unsigned short* buffer to a char * buffer (in the case where bpp == 16), i do not get the correct image (i can open the output image using NIH program and look at the output image). I do not know if there is other way of doing it using char* in both cases?
thanks |
|
#6
|
|||
|
|||
|
So, if imageInSize is the number of pixels, the following tries to read two times the number of pixels, since each pixel takes one byte:
CPP / C++ / C Code:
Is that what you meant to do? Dave |
|
#7
|
|||
|
|||
|
The following, from your original post, should not compile, assuming you are using the C++ standard <fstream> (what compiler did you use)?
CPP / C++ / C Code:
The inFile.read() must have a (char *) first argument. you could use CPP / C++ / C Code:
or (using the style that you used in the outFile.write statement): CPP / C++ / C Code:
Note that, since there are imageInSize pixels, you can allocate an array of imageInSize unsigned shorts, since each element of the array holds one pixel. The .read and .write statements always count bytes, so 2*imageInSize is the correct number of bytes to read and write for this case. (It does no harm to allocate more than you need, but is unnecessary and is misleading for future code maintainers.) Dave |
|
#8
|
|||
|
|||
|
I use the gnu g++ compiler. As you said, i casted the buffer array in read() (although i was not getting any compiler error for it) :
CPP / C++ / C Code:
Also, I fixed the problem that you mentioned for the case when bpp == 8 (the image size). Thank you very much, but I am still having the same problem. The output file is not stable. But in most cases i get an output file with 0 size or size that is much less than the reasonable one. The strange thing that happens is that the size of the output file changes when I run the program on another file!! I do not know how this can be possible. For example, say I run the program on FileOne, say the output file is 2891776. Then i run the program on FileTwo. When I do the ls -lt command or ls -s , the size of the FileOneOutPut has changed to 0 and and the size of the FileTwoOutput is less than what it is suppose to be. may be is it something with Linux that i am missing here?? Thank you |
|
#9
|
|||
|
|||
|
Without seeing your entire program, I can only suggest that you make sure you are reading all parameters correctly. Specifically use cout<< to see what values you are using for imagein.totalx and imagein.totaly, since these affect the size of the output file. Use cout<< to see what the total number of bytes is supposed to be. Etc., etc.
Running the program on a second input file obviously shouldn't affect the contents of the previous output. It's not a Linux thing, it's a program thing. (If you had bad hardware or a corrupted operating system or utility program, I would expect more drastic misbehaviour than just this program behaving badly.) General debugging hints: Sprinkle cout<< throughout the program to make sure it's doing what you had in mind. Are there ways that the program can determine exactly how many bytes are read and written? Stuff like that. Regards, Dave |
|
#10
|
|||
|
|||
|
I used cout in different palces to check the read and write. Everything seems to be fine...I do not know how to check the total number of bytes being read and written by the program?
This is suppose to be an easy read and write operations. I think the part that reads and writes the image data is fine because i use that part separately to read and write only the image data and i get the correct output. Also i use that part in another program that combines the images of different files in a directory without the header part , and that seems to be working as well. I have spent so much time on this and i am really fraustrated since i do not know what is going wrong. Here is my entire code. I really appreciate if you please take a look at it and let me know what you think. CPP / C++ / C Code:
Thank you very much |
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 |
| reading and writing to a file in C++ | mgp6q | C++ Forum | 17 | 02-Mar-2004 13:42 |
| [Tutorial] Standard I/O | aaroncohn | C Programming Language | 20 | 27-Feb-2004 22:07 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The