#read file value
44 messages Β· Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
@humble iron
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
the data is the mnist database of handwritten images. so the numbers written are 1 pixel, and range from 0 to 255
your hex view shows a long sequence of bytes, each byte from 0 to 255
use ifstream, read the chars, and use them
decrypting the bytes and parsing them into numbers with a neural network that is trained on hand draw images of numbers, well that's on you
its actually not chars, but rather unsigned 1 byte. so from 0 to 255.
you can see the chars side is gibberish
so I want to read the stream and fill it in a array
oh ok its a char because its one byte π
I have been able to successfully read the values from the file.
however there is a small prob.
when, I run this, i prints as 46831335. however in the hex editor, I can see there are 47040000 bytes. so where did the remaining bytes go?
when running in text mode, operator>> skips whitespace chars (\n ascii code 10, ' ' ascii code 32, and so on) and, \r (ascii code 13) gets completely skipped in MS Windows, thus corrupting your pixels
open ifstream in binary mode
and use the function read
to extract bytes in your pixel, or your local fixed length array
the problem is that the read function doesnt accept unsigned char values.
file.read(char*, int)
no, it does
pass a pointer
here I cant have unsigned char pixel, instead of char pixel
use reinterpret_cast<char*>(&pixel)
btw, what is this?
"grab the piece of memory where unsigned char pixel lives and pretend that a char lives there"
this is how you read bytes into variables
if your problem is solved type !solved that it will be marked as solved
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
calling read and reading 1 char is not very optimal, consider reading larger chunks to improve performance
ifstream underneath tries to read in chunks, load them in memory and give you chars one by one from there, but that's a call to read every single time
also, your calling of close() is redundant, ifstream has a destructor which will call close for you
don't be a C dev, be a C++ dev π
should I do
char pixels[47040000]
file.read(pixels, 47040000)
?
you are forgetting, the ammount of space for local variables is ~2MB, your program will explode before main starts
allocate memory from the heap first, then run your read
this will bloat your executable to be as large as this array in bytes, plz dont
first day