![]() |
|
#1
|
|||
|
|||
Working with binary fileshi,
i am working on some binary files. basically, i have to process every element of a binary file, then output the results to another binary file. there are some things which i cant understand: 1. if say my binary file is 1000 bytes in size, and i know that the file contains 1000 data points, can i assume that each data point is 1 byte in size? does that also mean i can read each element as a char, since sizeof(char) = 1? 2. if in the case that i read each element as char, can i directly do mathematical operations on it? or do i need to convert each char to int first, in which case the output will no longer be 1 byte per element, right? |
|||
|
#2
|
|||
|
|||
Re: working with binary filesQuote:
Quote:
Quote:
All operators that work on ints will work on chars. There may be issues of overflow if you are doing arithmetic on the byte values(or if you are doing equivalent stuff like shifting), and integer overflows are always ignored by C and C++ programs, but the operations themselves are well-defined. There might be a reason to explicitly convert the data values to ints (or floats or doubles) if you are going to do something like "multiply each data value by 4 and divide by 3," in which case working on a larger data item might reduce the chance of overflow. Similarly, if you are doing filtering with something like "add 10 data values together and divide by 10," you would (probably) want to convert data values to ints (or floats or doubles) before the operations. Stuff like that. Regards, Dave Footnote: The generally correct answer to questions like "do I need..." is (almost) always: "It depends." It depends on what the heck you are really going to do with the data and what you expect of the program's output. Since you didn't give us a program specification, I can only answer in generalities. I give a couple of examples where the answer is, "Probably yes." I am sometimes known as a kind of a specialist in vague generalities. I tried to be more vague in my "generally correct" answer, but that's the best I could come up with on the spur of the moment. Bottom line: "All generalities are false. Vague generalities are even more false." ---davekw7x |
|
#3
|
|||
|
|||
Re: Working with binary filesi am working with binary files that open with specific image viewers, so each data point is one pixel.
i want to read every data point (as char), do mathematical manipulations, which include: 1. comparing every point to some pre-fixed numerical value -- if the point is smaller than this value, leave it; if the point is bigger than this value, i want to change that point to 0 (yes, i am doing some sort of masking) 2. simple manipulations such as addition and multiplication, etc and finally output the processed data points (hopefully as char too, to match the output file size to the original file size) into a binary file that i will need to open with my image viewer again. for the masking part, i have tried CPP / C++ / C Code:
but this code simply left the majority of the data points untouched while randomly changing some to 0. any hints? Last edited by admin : 09-Apr-2009 at 04:40.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#4
|
|||
|
|||
Re: Working with binary filesQuote:
Quote:
Quote:
Randomly? Really? How about this: 1. create a file with known values in all of the places. 2. run the file through your program 3. look at what happens. Here's what I might do: 1. Write character values 0x00 through 0xff to the file, Maybe with something like CPP / C++ / C Code:
2. Make a program to read and display all of the characters from the file, one at a time. I mean you could put them into an array, but why bother? CPP / C++ / C Code:
Note that my compiler (and most that you are likely to run across these days) uses signed chars by default. Here's the output of this program when it reads the file written by the first example: Code:
Now run this file through a program that converts byte values greater or equal to than some threshold to zero and writes the results to another file. You could modify your program to read and write 256 bytes. I would probably not hard-code a fixed file size in the program, but for testing purposes, you can just go with what you have. See Footnote. If I set the threshold to 80, for example, the result would look like Code:
Now, if the bytes in your file are supposed to be unsigned chars (which I suspect is the case for image files), then declare inchar to be an unsigned char in the program that reads and displays the file. (If you are going to use an array and compare values in the array to an arithmetic value, declare the array to be unsigned chars.) Then the file written by the first example would be displayed by the modified reading program as Code:
And the file after being run through the modified "threshold" program would be displayed as Code:
Bottom line: After testing my threshold program for all possible bytes that could occur in the file by using a known file, I might run the program on the (larger) actual image file. If the bytes really were changed "randomly," I might look for some kind of undefined behavior (reading/writing beyond the limits of assigned memory. Stuff like that.) Regards, Dave Footnote: I would never write a program that assumed files are opened and assumes that files are read correctly. I really mean this: Never. Period. I showed how I test for open files. If I were going to read into an array using fread(), I might do something like CPP / C++ / C Code:
I would also check the number of bytes written by fwrite and report this also. |
|
#5
|
|||
|
|||
Re: Working with binary filesmy data points are in unsigned char - they run from 0-255.
i will try out your suggestions and get back. thanks |
|
#6
|
||||
|
||||
Re: working with binary filesQuote:
...unless the data is compressed MxB |
|
#7
|
|||
|
|||
Re: working with binary filesQuote:
I was responding to the Original Post: Quote:
1000 bytes divided by 1000 data points = 1 byte per data point. 1000 data points divided by 1000 bytes = 1 data point per byte. Or am I missing something? Regards, Dave |
|
#8
|
||||
|
||||
Re: working with binary filesQuote:
Lighten up Dave...it was just a joke. (note big smilie) Obviously, "it could be" something else if the data was compressed, but (also obviously) highly unlikely considering the provided info. MxB |
|
#9
|
|||
|
|||
Re: working with binary filesQuote:
Quote:
I tend to overlook emoticons as well as the execreble and ubiquitous presence of "lol" in e-mail and other messages these days. My poor little peanut brain just filters them out unless someone draws particular attention. (Thank you, at least, for not putting in a dancing banana!) Oh, well... Regards, Dave |
|
#10
|
|||
|
|||
Re: Working with binary filesrelax guys, turns out its a simple mistake that was bugging me - i have declared a signed char array to read the data, so that means i am only allowing values from -127 to 128 to get into my array, whereas my data runs from 0-255, which means i have to use unsigned char instead. thx for pitching in
|
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Named virtual host not working | Johnnyrotton | Apache Web Server Forum | 4 | 04-Sep-2007 21:32 |
| Bloodshed Dev C++ Project Options | JdS | C++ Forum | 6 | 11-Nov-2005 18:23 |
| Reading and writing binary files in certain format | Dream86 | C++ Forum | 10 | 06-Aug-2004 11:38 |
| Can't view pages from another machine on the Intranet | aevans | Apache Web Server Forum | 9 | 14-May-2004 03:26 |
| Help with binary files (encryption?) | pablowablo | C++ Forum | 6 | 28-Apr-2004 23:47 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The