I want to read an image file as binary, then save it again in its binary form, however i am having some problems as i cant seem to get it to read the file as binary.
1. You hit the EOF because you never reset the file pointer back to the beginning.
2. You're checking that fp!=NULL after fseek()ing and ftell()ing.
3. Use fread() to read from binary files.
4. Use fstreams if you're using C++.
Note that your fopen() flags will open the file even if it didn't already exist. In that case, it will create an empty file. Also, that even though you're reading the file as binary, you're writing it as text. You should limit yourself to fread() and fwrite() when working with binary files.
EDIT: That looks perfectly fine, to me, although it doesn't seem to be a JPEG file.
Oh, one more thing. Line 13 will overflow 'file' if size is >1000.
If I may make an observation, you should use dynamic memory allocation to handle the data buffer. Also, your variable names need a little help (you named your buffer "file"). The following is a little lengthy, but correct. If you are clever, you can make it much shorter. ;-)
Thankyou very much, is it possible to save a version which consists only of binary?
isnt the file length better unsigned btw?
'(beware the 2Gb limitation, though)'
Is there any way around that, out of curiosity?
I have tried changing this:
if (!fwrite( (long*)long(buf), len, 1, fp ))
I know chars are already ints, but i want to make the application just save the numbers, so i can manipulate it better. That method just saves it in its normal form with trailing randomness.
Is there any way to save all of the buf data as an int, not english text.
[edit]
Ok, i got it kinda working, but it shows different results each time... lol.
The idea was to convert it from Char to Int
So i tried:
long * value = (long*)long(buf);
But it displayed normal text too. Then i tried (which is as far as i have gotton).
1 2 3 4 5 6 7 8 9
// Write the entire buffer to file
char * d = (char*)malloc( (unsignedlong)buf );
itoa(long(buf),d,2);
if (!fwrite( d, len, 1, fp ))
{
free( buf );
fclose( fp );
return 0;
}
i realize 2 is binary, and 8 is normal characters, however, it always gives me a different value and i have no idea why. im a bit of a noob with pointers and i did my best to get them to work.
Is there any way around [the 2 GiB limit], out of curiosity?
Other than using a 64-bit system, a file system library (e.g. Boost::filesystem. Or maybe it was a different one. I know it was possible with Boost), or direct system calls, no.
I agree with Duoas; the code does make any sense. What are you trying to do?
I added comments, but im trying to make it save as either binary, or as a number, not text.
1 2 3 4 5 6 7 8
char * d = (char*)malloc( (unsignedlong)buf ); //Make a char which has the size of buf
itoa(long(buf),d,2); //we then put the binary form of buf into d
if (!fwrite( d, len, 1, fp )) //we write d
{
free( buf );
fclose( fp );
return 0;
}
I do end up with the right result, if i put 2 it writes binary, if i put 8 it writes it as an int, however, the number is always different...
"Binary" has very specific meanings. Don't use it so lightly.
What you're trying to do is write the textual representation of the binary or decimal representation of the character codes. That can be done this way: