Sign in to follow this  
m4gnus

ofstream.write() doesn't write anything

Recommended Posts

The topic pretty much sums it up. In my exporter i have a line which *should* write all the vertices to a file but when i remove the ofstream::write call the output file gets even larger! the pointer i pass to the function is 0x27666470(looks ok to me), my vertexsize is 56 an the vertexcount is 2222. But the file is just 68 bytes big(which is little more than 1 vertex! How can this be possible? the file opened okay(it is created) but still he doesn't want to write the vertices. My code: vertex writing code
std::cerr<<"Final Vertex Count:"<<vertCount<<std::endl; //returns 2222
				std::cerr<<"Vertex Size:"<<sizeof(complVertex)<<std::endl; //returns 56
				std::cerr<<"VertexPointer addresse:"<<pVerts<<std::endl; //returns 27666470
				ofs.write(reinterpret_cast<char*>(pVerts),sizeof(complVertex)*vertCount); //when i make this line a comment the size of the file gets 20bytes bigger( 88bytes )


all file writing code:
				//vertices:
				std::ofstream ofs(filepath.asChar(),std::ios::out | std::ios::binary);
				VertexChunkHeader.dwNumVertices=DWORD(vertCount);
				ofs.write(reinterpret_cast<char *>(&VertHeader),sizeof(VertHeader));
				ofs.write(reinterpret_cast<char *>(&VertexChunkHeader),sizeof(VertexChunkHeader));
				ofs.write(reinterpret_cast<char*>(&desc[0]),sizeof(ngVertexDesc)*numDesc);

				std::cerr<<"Final Vertex Count:"<<vertCount<<std::endl;
				std::cerr<<"Vertex Size:"<<sizeof(complVertex)<<std::endl;
				std::cerr<<"VertexPointer addresse:"<<pVerts<<std::endl;
				ofs.write(reinterpret_cast<char*>(pVerts),sizeof(complVertex)*vertCount); //<--the strange line


				//indices:
				IndexChunkHeader.dwNumIndices=indexCount;
				std::cerr<<"Final Index Count:"<<indexCount<<std::endl;
				IndHeader.dwDataSize=sizeof(IndexChunkHeader)+IndexChunkHeader.dwNumIndices*sizeof(int);
				ofs.write(reinterpret_cast<char *>(&IndHeader),sizeof(IndHeader));
				ofs.write(reinterpret_cast<char *>(&IndexChunkHeader),sizeof(IndexChunkHeader));

				ofs.write(reinterpret_cast<char*>(pIndices),sizeof(int)*indexCount);

				
				//for(int l=0;l<VertexIndices.size();l++) {
				//std::vector<short>::pointer ptr = &VertexIndices[0];
				//ofs.write(reinterpret_cast<char *>(ptr),sizeof(int)*VertexIndices.size());
				//std::vector<short>::pointer ptr2 = &UVIndices[0];
				//ofs.write(reinterpret_cast<char *>(ptr2),sizeof(int)*UVIndices.size());

		
				ofs.close();


the only reason for the file getting smaller i could think of is that the rest of the data which should be written to the file after the vertices isn't processed. So is there some way to "break" the file so that no more data can be written to it? Can somebody explain me how this is possible? regards, m4gnus [Edited by - m4gnus on June 9, 2006 6:47:19 AM]

Share this post


Link to post
Share on other sites
Did you forget to open the file in binary mode, such that an EOF character (Ctrl-Z, ascii 26 on Windows; Ctrl-D, ascii 4 on Unix) which happens to compose part of the binary vertex representation gets interpreted as the actual end of file?

Share this post


Link to post
Share on other sites
You might try checking the status of ofs after each write. It is possible that an error is ocurring that causes the ofs object to become bad. The fact that nothing gets written to the file after the attempt to write pVerts would seem to point to an error ocurring in that write call.

Share this post


Link to post
Share on other sites

thanks for the replies
Quote:

pVerts is 1D array complVertex structure?

yes
it's a pointer to the first element of a std::vector<complVertex>.

Quote:

Did you forget to open the file in binary mode, such that an EOF character (Ctrl-Z, ascii 26 on Windows; Ctrl-D, ascii 4 on Unix) which happens to compose part of the binary vertex representation gets interpreted as the actual end of file?

don't think so. the ofstream is opened with:
std::ofstream ofs(filepath.asChar(),std::ios::out | std::ios::binary);

Quote:

You might try checking the status of ofs after each write. It is possible that an error is ocurring that causes the ofs object to become bad. The fact that nothing gets written to the file after the attempt to write pVerts would seem to point to an error ocurring in that write call.

just checked. Yes the file get's bad. What can cause that? Maybe there something wrong with getting the pointer(it's a little more complex than just a pointer to a vector in the same scope).

I have a function which computes uv coordinates,tangents and binormals for every vertex and it's ouput is 2 pointers and 2 ints. one pointer for the vertexindices,one for the vertices itself,1 int for the indices count and 1 int for the vertices count.
To output those i need to pass a double pointer to the function like this:
createVertices(/*other parameters*/,complVertex **ppOutVerts,int **ppOutIndices,int *numVerts,int *numIndice);

and in the function i do:
(*ppOutVerts)=&vertices[0];
(*numVerts)=vertices.size();

(*ppOutIndices)=&complIndices[0];
(*numIndice)=complIndices.size();

and call it like this:
complVertex *pVerts;
int *pIndices;
int vertCount,indexCount; createVertices(/*parameters*/,&pVerts,&vertCount,&pIndices,&indexCount);

is that correct?

regards,
m4gnus

Share this post


Link to post
Share on other sites
Quote:
Original post by m4gnus
...
and in the function i do:
(*ppOutVerts)=&vertices[0];
(*numVerts)=vertices.size();

(*ppOutIndices)=&complIndices[0];
(*numIndice)=complIndices.size();


Are vertices and complIndices local variables to that function? If so, then when you go to output them those pointers will be invalid.

Share this post


Link to post
Share on other sites
Quote:

Are vertices and complIndices local variables to that function? If so, then when you go to output them those pointers will be invalid.

they are!
But how do i solve that? i want to be able to call that function more than once without problems(so moving the declaration of the vectors above the function isn't an option).
Should i declare them static?

Edit: ok declared them static now and the exporter works like a charm.
Thank you!

regards,
m4gnus

[Edited by - m4gnus on June 10, 2006 2:05:16 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this