wrote to binary file, but...

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

wrote to binary file, but...

 
0
  #1
Mar 14th, 2009
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?

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: wrote to binary file, but...

 
0
  #2
Mar 14th, 2009
>>
  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
  1. writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb[0]) ,sizeof(&img_pix[i].rgbb[0] );
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: wrote to binary file, but...

 
0
  #3
Mar 14th, 2009
Isn't a pbm a text file?
Shouldn't you be writing your numbers as text, with:
writer << img_pix[i].rgbb[0];
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: wrote to binary file, but...

 
0
  #4
Mar 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: wrote to binary file, but...

 
0
  #5
Mar 14th, 2009
Originally Posted by nucleon View Post
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..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: wrote to binary file, but...

 
0
  #6
Mar 16th, 2009
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:
  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1487
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: wrote to binary file, but...

 
0
  #7
Mar 16th, 2009
Originally Posted by Icebone1000 View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: wrote to binary file, but...

 
0
  #8
Mar 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1487
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: wrote to binary file, but...

 
0
  #9
Mar 16th, 2009
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
  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];

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
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: wrote to binary file, but...

 
0
  #10
Mar 16th, 2009
"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 :
  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 :
  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:
  1. writer.write(&img_pix[i].rgbb[0],sizeof(img_pix[i].rgbb[0]));
the file gets 914KB
using this:
  1. writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb) ,sizeof(img_pix[i].rgbb));
the file gets 2,67 MB
using this:
  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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC