943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1558
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 16th, 2009
0

Re: wrote to binary file, but...

BTW, in this case im using just rgbb[0] because im using a PBM file, with is a black and white and just use one bit per pixel, so i dont need rgbb[0] rgbb[1] rgbb[2]..but i keep the array because my program can open all types of pnm files(ppm, pbm, pgm, ascii and binary modes)
With the ascii files im having no problems..
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Mar 16th, 2009
0

Re: wrote to binary file, but...

From what you posted earlier it still looks like rgbb is uninitialized. Do you write rgbb in text files too?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 16th, 2009
0

Re: wrote to binary file, but...

From what you posted earlier it still looks like rgbb is uninitialized. Do you write rgbb in text files too?
For text files I use the int rgb[3]....
I dont get very well what do you mean, I do not initialize rgbb with default values, i just start use it when reading the pixel map from the original file:
C++ Syntax (Toggle Plain Text)
  1. readPNM.read(&img_pix[i].rgbb[0],1);
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Mar 16th, 2009
0

Re: wrote to binary file, but...

At this point I think you probably need to use your compiler's debugger, put a break point on that write line and inspect the value of that array. You have only posted one line from your program so its pretty impossible for anyone to tell you the solution to the problem. Possibly its not that line at all but somewhere else in the program, such as memory corruption.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 16th, 2009
0

Re: wrote to binary file, but...

It would seem impossible for your original code (in post 1) to write more than width * height + 25 bytes (give or take a few bytes for different widths since you are writing these as text), so if you are getting 8 times this many bytes your problem must lie elsewhere. You need to attach your (minimal) program and data file.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 16th, 2009
0

Re: wrote to binary file, but...

using this :
C++ Syntax (Toggle Plain Text)
  1. writer.write(reinterpret_cast<char*>(&img_pix[i].rgb) ,sizeof(img_pix[i].rgb));
the file gets 10,7 MB o__o"
using this :
C++ Syntax (Toggle Plain Text)
  1. writer.write(reinterpret_cast<char*>(&img_pix[i].rgb[0]) ,sizeof(img_pix[i].rgb[0]));
the file gets 3,4 MB ..
using this:
C++ Syntax (Toggle Plain Text)
  1. writer.write(&img_pix[i].rgbb[0],sizeof(img_pix[i].rgbb[0]));
the file gets 914KB
using this:
C++ Syntax (Toggle Plain Text)
  1. writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb) ,sizeof(img_pix[i].rgbb));
the file gets 2,67 MB
using this:
C++ Syntax (Toggle Plain Text)
  1. writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb[0]) ,sizeof(img_pix[i].rgbb[0]));
the file gets 914 KB
It seems that you are desperately trying various ways to write the file.

Maybe relax and try to master e.g. a binary .pnm file of a very small size, say 2x2 pixels hence being easily viewed in any hexeditor at a glance (notepad and wordpad surely are poor choices for checking the files' content).

Try for example the following struct for reading/writing
C++ Syntax (Toggle Plain Text)
  1. struct Pixel
  2. {
  3. // 3 bytes represents a single pixel
  4. unsigned char rgb[3];
  5. };

Then when you read in the file you might
C++ Syntax (Toggle Plain Text)
  1.  
  2. // ... code here to read in the file's header ...
  3.  
  4. // allocate enough memory to read in rest of the file
  5. const int pixels = width * height;
  6. Pixel * p = new Pixel[pixels];
  7.  
  8. for(int ii = 0; ii < pixels; ++ii)
  9. {
  10. // read 3 bytes at a time
  11. if( ! file.read(reinterpret_cast<char *>(p[ii].rgb), 3))
  12. {
  13. // should not happen, handle error ...
  14. break;
  15. }
  16. }
  17.  
  18. // all done

Then to write the file, you can iterate over the array issuing

file.write(reinterpret_cast<const char *>(p[ii].rgb), 3)

Remember that the second parameter to both read() and write() determines how many bytes each I/O operation involves.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 18th, 2009
0

Re: wrote to binary file, but...

Im attaching a .zip file with the cpp and the image files im using to test the program..**

mitrmkar, but I think this is exactly what Im doing now, I tried with unsigned char and the method you said, and got the same 914KB result..

"determines how many bytes each I/O operation involves."
In the case of a pbm file, the map of pixels is just one bit per pixel( black or white)..

I still dont get how can the problem come just AFTER all the writing (or
reading)..?


Edit: Ah..I dont know how I can see whats going on by debugging it, since the array is dynamic allocated I cant watch it, and I dont have idea of how can I check the fstreams...

**Damn, Im getting upload error with the attachment of the file, is this temporary or I have problems?
Last edited by Icebone1000; Mar 18th, 2009 at 9:06 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Mar 18th, 2009
0

Re: wrote to binary file, but...

C++ Syntax (Toggle Plain Text)
  1. + rgbb 0x0044714c "øÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍþÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͸ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ" unsigned char [3]

Hey, look this..thats a watching to img_pix[10000] on the reading momment...Im imagining it are getting right the one bit it suppose to, but since i have a byte on rgbb[0] it let the empty spaces after the bit with junk..And when Im writing to a new file, it writes just the bits, and put all this junk just after it??...Am Im imagining too much?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Mar 18th, 2009
0

Re: wrote to binary file, but...

C++ Syntax (Toggle Plain Text)
  1. + rgbb 0x0044714c "øÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÿÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍþÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͸ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ" unsigned char [3]

And when Im writing to a new file, it writes just the bits, and put all this junk just after it??...Am Im imagining too much?
Now remember the second parameter to write() , that specifies how many bytes are written.

PS. Maybe you could try to re-post your code
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 18th, 2009
0

Re: wrote to binary file, but...

I attached the files.

"and put all this junk just after it??"

I get it, but it puts the junk after ALL the bits, not just after each one, that is what Im not understanding.
And how can I say to read and write to get just a single bit?
Attached Files
File Type: zip myPNMtest.zip (362.7 KB, 9 views)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pass variables to cmd?
Next Thread in C++ Forum Timeline: Code::Blocks: Issue building and running C++ progs between varying versions?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC