View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# ofstream.write() doesn't write anything

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1m4gnus  Members

Posted 08 June 2006 - 11:26 PM

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
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);
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;
ofs.write(reinterpret_cast<char*>(pVerts),sizeof(complVertex)*vertCount); //<--the strange line

//indices:
std::cerr<<"Final Index Count:"<<indexCount<<std::endl;

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]

### #2kuphryn  Members

Posted 09 June 2006 - 05:40 AM

pVerts is 1D array complVertex structure?

Kuphryn

### #3Zahlman  Members

Posted 09 June 2006 - 06:16 AM

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?

### #4Dave Hunt  Members

Posted 09 June 2006 - 06:37 AM

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.

### #5m4gnus  Members

Posted 10 June 2006 - 12:11 AM

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

### #6Dave Hunt  Members

Posted 10 June 2006 - 06:04 AM

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.

### #7m4gnus  Members

Posted 10 June 2006 - 08:05 AM

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]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.