| | |
wrote to binary file, but...
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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?
edit: My program just loads the file , store its information and save it..
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)
ofstream writer("out.pbm", ios::binary); writer << MV[0] << MV[1] << '\n'; writer <<"# Created by me"<< '\n'; writer << width << ' ' << height << '\n'; for(long i=0; i < (width*height); i++){ writer.write(&img_pix[i].rgbb[0],1); } 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.
>> 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)
writer.write(&img_pix[i].rgbb[0],1);
C++ Syntax (Toggle Plain Text)
writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb[0]) ,sizeof(&img_pix[i].rgbb[0] );
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
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:
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)
... ifstream readPNM; ... for(long i=0; i < (width*height); i++){ readPNM.read(&img_pix[i].rgbb[0],1); }
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.
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
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.
•
•
•
•
Its a pointer to struct PIXELS with :
int rgb[3];//for ascii
char rgbb[3];//for binary
C++ Syntax (Toggle Plain Text)
struct PICX { union { int rgb[3];//for ascii char rgbb[sizeof(int)];//for binary } };
>>PIXELS *img_pix = new PIXELS[width*height];
•
•
•
•
writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb[0]) ,sizeof(&img_pix[i].rgbb[0] );
[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)
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.
"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 :
the file gets 10,7 MB o__o"
using this :
the file gets 3,4 MB ..
using this:
the file gets 914KB
using this:
the file gets 2,67 MB
using this:
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?
- 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)
writer.write(reinterpret_cast<char*>(&img_pix[i].rgb) ,sizeof(img_pix[i].rgb));
using this :
C++ Syntax (Toggle Plain Text)
writer.write(reinterpret_cast<char*>(&img_pix[i].rgb[0]) ,sizeof(img_pix[i].rgb[0]));
using this:
C++ Syntax (Toggle Plain Text)
writer.write(&img_pix[i].rgbb[0],sizeof(img_pix[i].rgbb[0]));
using this:
C++ Syntax (Toggle Plain Text)
writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb) ,sizeof(img_pix[i].rgbb));
using this:
C++ Syntax (Toggle Plain Text)
writer.write(reinterpret_cast<char*>(&img_pix[i].rgbb[0]) ,sizeof(img_pix[i].rgbb[0]));
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?
![]() |
Similar Threads
- Problem with binary data reading (C)
- How to make EXE file from assembly? (Assembly)
- VB 6.0. Creating a file for random access (Visual Basic 4 / 5 / 6)
- Dynamic array (C++)
- Pascal -> C++; Binary, convert? (C++)
- Custom Class - File I/O (C++)
- reading a file into code (Java)
- Array limit (C)
Other Threads in the C++ Forum
- Previous Thread: Pass variables to cmd?
- Next Thread: Code::Blocks: Issue building and running C++ progs between varying versions?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






