-1

I tring to export a mesh(softbody) to .stl format.I used the following code.Please help I'm new to these stuffs.

const CHAR* fileName = "D:\\Images\SCREENE.stl" ;
FILE* file = fopen(fileName,"rb");
unsigned char *buffer=0;
unsigned char *temp=0;

buffer[0]= (const char)"#vertices:\n  #indices\n";
for(int i = 1 ; i < softWorld->getSoftBodyArray().size(); i++) 
{
   buffer=(unsigned char*)softWorld->getSoftBodyArray()[i];

}
fwrite(buffer, sizeof(unsigned char), softWorld->getSoftBodyArray().size(), file);

fclose(file);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • To begin with, shouldn't `"D:\\Images\SCREENE.stl"` be `"D:\\Images\\SCREENE.stl"` (add one more `\`)? – MikeCAT Nov 23 '15 at 10:46
  • 1
    Assigning some integer converted from the pointer in `buffer[0]= (const char)"#vertices:\n #indices\n";` make no sense. Also, do not access `buffer[0]` since `buffer` is `NULL` here. – MikeCAT Nov 23 '15 at 10:47
  • If you need to use C-style type-casting to silence a compiler error or warning, you are probably doing something you should not be doing. Remove the casting and think about what the error or warning might actually mean. – Some programmer dude Nov 23 '15 at 10:49
  • Then for your question... What *is* your question? What is the problem (besides what's been noted above) you have with your code?Does it build? Does it run? Does it produce some unexpected output or results? – Some programmer dude Nov 23 '15 at 10:51
  • 1
    it produce crash because you open file for reading ("rb"), but you trying to write in it. – Yuriy Orlov Nov 23 '15 at 10:56
  • @YuriyOrlov Oh that would probably just cause `fwrite` to fail, there are much more serious errors in the code, which would be perfectly clear if the OP just didn't silence the compiler by those casts. – Some programmer dude Nov 23 '15 at 10:57
  • I already said tat i am new to these things..i beg your pardon if i post a very bad question..if anybody can help me..please please help – Greeshma Panicker Nov 23 '15 at 11:04
  • You might want to check out [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Nov 23 '15 at 11:11

1 Answers1

0

There are a few problems, all related to pointers and that leads to undefined behavior.

Lets start with this:

buffer[0]= (const char)"#vertices:\n  #indices\n";

Here you take a pointer to a string literal, and tell the compiler to treat this pointer as a single character, and assign it to buffer[0], which at this point is a null pointer. Dereferencing a null pointer leads to undefined behavior, and is most likely the cause of the crash.

But it's not the only bad thing you're doing. Lets take this:

buffer=(unsigned char*)softWorld->getSoftBodyArray()[i];

Judging by your code softWorld->getSoftBodyArray() probably returns either an std::array or a std::vector object. That means you take an element from this array or vector, and cast it to pointer and store it in buffer. Again a case of undefined behavior because a single character (I assume) is not a pointer to something. You also do this in a loop, each iteration you loose the previous assignment. This won't cause a crash though, but then you do

fwrite(buffer, sizeof(unsigned char), softWorld->getSoftBodyArray().size(), file);

Here buffer is not a valid pointer. Dereferencing this invalid pointer is again undefined behavior and could cause a crash.


There are also other things with your code that makes it suspect, like why are you using the old C file input/output functions instead of C++ streams?

You also open this file in binary mode, which indicates that you write binary data to the file, and yet you seem to be wanting to write some text also (I'm just guessing here, I don't know anything about the file format).

And finally, you want to write to the file, but you open it in read-only mode.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621