Archived

This topic is now archived and is closed to further replies.

destrier

problems with glut and fstream file io

Recommended Posts

Hi I''m reading pixel data with glReadPixels while using glut, I want to save the array of pixel data in a text format in a file using fstream. fstream will create the file but will not actually write the data to a file from within glutmainloop do you know why? and how to fix it? code: /*the func below is called from glutKeyboardFunc*/ void send_image_data_to_text(void) { RGB value; int x,y; fstream examplefile ("pmg.txt",ios::out); if (examplefile.is_open()) { examplefile << "This is a line2.\n"; examplefile << "This is another line.\n"; examplefile.close(); cout << "ha"; for(x=0; x<70; x++) for(y=0; y<70; y++) { value = pic[0].getPixel(x, y); examplefile << value.r << value.g << value.b; examplefile << "fuck!"; cout << value.r << value.g << value.b; } examplefile.close(); } } this function works fine in a normal VC++ project or in main before glutmainloop is called. This is how I know there is a problem between fstream and glut. If you have any info I would love to here from you.

Share this post


Link to post
Share on other sites
examplefile << value.r << value.g << value.b;

stick a breakpoint here , does the line get reached?

ild write it like so (provided the data is stored as floats)

examplefile = fopen("jnnj.txt", "wt")
fwrite( pic[0].data, 70*70*3, sizeof(float), examplefile );
fclose( examplefile );

it will be hundred times quicker

http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites
yes zedzeek your code would be faster but I want to write a formated txt file. I have put a break point inside of the loop and it reaches the code that sends the data to the file. But still the data never showes up in the file.

I found a half-ass solution to my problem but the program still doesn''t do what I want it to do.
here is the solution maybe you can tell me why it works:
ok, I know that the data is going out to the console window fine so I know glreadpixels is working.
but that is not of any consequence because I cant even get the test data out to the file
examplefile << "line 1\n";
examplefile << "line 2\n";
so this is how I make it work
I declare the file handle (fstream examplefile ("pmg.txt",ios::out) as a global;
(you would think that would solve the problem but it doesn''t with out one more call)
I call (examplefile << " " in main just before calling glutmainloop();.
then the data writes to the file just fine. I guess some how that initializes the file to be written.
why is this not a satisfactory solution?
because I need to open files that are user defined so they can''t be hard coded as globals.
So what I''m trying to do is open the file from a function inside of glutmainloop.
as you may know once you are in glutmainloop you can''t leave it with out hacking the source code.
So there are the things that I have discovered since I first e-mailed. Perhaps this info will tell you the answer to my problem.
I for one don''t know enough about the fstream class to understand whats going on between glut and the other libraries. To even comprehend why calling examplefile << " "; before entering the glut event loop works, is beyond me.
I''m desperately trying to finish the program for a class.
so thank you again for offering your help. You have know idea what it means to me.

Share this post


Link to post
Share on other sites
I know this is bad practice but have you tried making the fstream object global and opening it in main? Then closing it at the end of main? Ive had this happen a few times while doing things not related to glut and this usually solved the problem even though globals are evil! Also, you should call the open method on the file
   
fstream examplefile.open("pmg.txt",ios::out);


Edited by - xds4lx on February 3, 2002 6:02:27 PM

Share this post


Link to post
Share on other sites
im not a fan of the iostream so ill write it otherwise

FILE *examplefile = fopen("jnnj.txt", "wt")
for(x=0; x<70; x++)
{
for(y=0; y<70; y++)
{
value = pic[0].getPixel(x, y);
fprintf( examplefile, "%f %f %f\n",value.r,value.g,value.b);
fflush( examplefile );
}
}
fclose( examplefile );

perhaps its not flushing though it should if u close a file

http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites
Alas it seems fstream just will not work with GLUT
this is fine it seems that every one prefers stdio.h
in any case.

thanks for the tip your code works great.

pixel = new unsigned char [(3*(nRows*nCols))];
if(!pixel){exit(-1);}
glReadPixels(x, y, nCols, nRows, GL_RGB, GL_UNSIGNED_BYTE, pixel);

FILE *examplefile = fopen("PPM.txt", "wt");
fprintf( examplefile, "PPM\nP2\n");
for(y=0; y {
for(x=0; x<=(nCols*3)-3; x=x+3)
{
r = static_cast(pixel[x+(y*nCols*3)]);
g = static_cast(pixel[x+(y*nCols*3)+1]);
b = static_cast(pixel[x+(y*nCols*3)+2]);
fprintf( examplefile, " %d %d %d :",r,g,b);
fflush( examplefile );
}
fprintf( examplefile, "\n");
fflush( examplefile );
}
fclose( examplefile );


Edited by - destrier on February 9, 2002 12:10:31 PM

Edited by - destrier on February 9, 2002 12:11:35 PM

Edited by - destrier on February 11, 2002 3:37:06 PM

Share this post


Link to post
Share on other sites