943,960 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1559
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 14th, 2009
0

wrote to binary file, but...

Expand Post »
The file gets more kB then the original ( 114KB to 914KB)
And something weird: when I try open the new file with the notepad, it loads forever and i get a program not responding message when I try to close it, with the original file it loads ok..

By the way the new file opens ok, its a pbm image file..

I guess Im missing something that I have to do when writing to binary files?

C++ Syntax (Toggle Plain Text)
  1. ofstream writer("out.pbm", ios::binary);
  2. writer << MV[0] << MV[1] << '\n';
  3. writer <<"# Created by me"<< '\n';
  4. writer << width << ' ' << height << '\n';
  5.  
  6. for(long i=0; i < (width*height); i++){
  7.  
  8. writer.write(&img_pix[i].rgbb[0],1);
  9.  
  10. }
  11.  
  12. writer.close();

edit: My program just loads the file , store its information and save it..
Last edited by Icebone1000; Mar 14th, 2009 at 2:14 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Mar 14th, 2009
0

Re: wrote to binary file, but...

>>
C++ Syntax (Toggle Plain Text)
  1. writer.write(&img_pix[i].rgbb[0],1);
I don't know what is the type of img_pix but if it is not char, you better use a cast to char
C++ Syntax (Toggle Plain Text)
  1. writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb[0]) ,sizeof(&img_pix[i].rgbb[0] );
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Mar 14th, 2009
0

Re: wrote to binary file, but...

Isn't a pbm a text file?
Shouldn't you be writing your numbers as text, with:
writer << img_pix[i].rgbb[0];
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 14th, 2009
0

Re: wrote to binary file, but...

Its a pointer to struct PIXELS with :
int rgb[3];//for ascii
char rgbb[3];//for binary

PIXELS *img_pix = new PIXELS[width*height];


I was thinking if a have to 'finish' the file in some way( like finishing a string with '\0'..?
Last edited by Icebone1000; Mar 14th, 2009 at 2:56 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Mar 14th, 2009
0

Re: wrote to binary file, but...

Click to Expand / Collapse  Quote originally posted by nucleon ...
Isn't a pbm a text file?
Shouldn't you be writing your numbers as text, with:
writer << img_pix[i].rgbb[0];
all PNM files have an ascii and a binary version, in my case is the binary..
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...

Hi again
I opened the new file with word pad, the problem is theres a lot of memory junk being writed on the end of the file (ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ...) lots and lots

Any idea of what can I be doing wrong?
Im reading the original file in the same way Im writing:
C++ Syntax (Toggle Plain Text)
  1. ...
  2. ifstream readPNM;
  3. ...
  4. for(long i=0; i < (width*height); i++){
  5.  
  6. readPNM.read(&img_pix[i].rgbb[0],1);
  7.  
  8. }
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...

Hi again
I opened the new file with word pad, the problem is theres a lot of memory junk being writed on the end of the file (ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ...) lots and lots

Any idea of what can I be doing wrong?
You aren't doing anything wrong. You can't view binary files with Notepad because they contain binary data that Notepad doesn't know how to display. Notepad can only display text files, not binary files.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Mar 16th, 2009
0

Re: wrote to binary file, but...

no no no no no..you get it wrong
The new file are getting bigger because theres a lot of memory junk being writed on it, comparing with the original one, with have no "ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ" at the end..get it?


I just opened it on wordpad to see whats going on
Last edited by Icebone1000; Mar 16th, 2009 at 10:04 am.
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...

Quote ...
Its a pointer to struct PIXELS with :
int rgb[3];//for ascii
char rgbb[3];//for binary
why not just use a union, which means the same memory location could be accessed by either an int or char array
C++ Syntax (Toggle Plain Text)
  1. struct PICX
  2. {
  3. union {
  4. int rgb[3];//for ascii
  5. char rgbb[sizeof(int)];//for binary
  6. }
  7. };

>>PIXELS *img_pix = new PIXELS[width*height];

Quote ...
writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb[0]) ,sizeof(&img_pix[i].rgbb[0] );
Does rgbb contain anything? Is it just an uninitialized array? Use the union concept mentioned above might resolve that if your program populates the int first.

[edit]
On second thought, if all you use rgbb for is to write out to the file, then you don't need it at all nor would you even need the union
C++ Syntax (Toggle Plain Text)
  1. writer.write(reinterpret_cast<char*>(&img_pix[i].rgb) ,sizeof(img_pix[i].rgb));
Last edited by Ancient Dragon; Mar 16th, 2009 at 10:20 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Mar 16th, 2009
0

Re: wrote to binary file, but...

"Does rgbb contain anything?"
- If the PNM file Im trying to open is a Binary one, so I use rgbb to read the pixel map, and so write it to a new file( I though is better use chars to get binary stuff..?) but i have to save it in the same way ( binary mode )with no modification, just load and save it.

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

But with ALL the options the file keep being opened ok, with no corruption or image weird problems( since the file header have its width and height and the problem is just junk after the pixel map )
I dont understand that, how can the junk be added just at the end? isnt it suppose to add the junk after each bit?
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