ofstream doesn't write good to file

Aug 31, 2010 at 11:07pm
Sometimes it does, but some other times it doesn't...See this picture:
I had a pop-up requesting for a path. This path string I converted into a char* and then I write this char to a file. Then I was taking only the filename using a function from another library and then I was setting this string st text to a Button in my app
I noticed that sometimes the button had filenames like hello.mp3- or hello.mp30 or hello.mp3` or hello.mp3P, hello.mp3P or other letters
I performed a check to see where the string is wrongly parsed and the result was when ofstream was writing it to the file. See this screenshot and tell me your opinion:
http://a.imageshack.us/img228/3396/screenshot5f.png

It is strange. Isn't it?
Sep 1, 2010 at 9:56am
I smell buffer overflow... I can't tell more with that much code. Maybe someone else can...

Question: is realpath a constant? If not, make it one and see if the problem persists.

Suggestion: Use std::strings instead of (const) char pointers/arrays to hold your strings.

Info on std::string -> http://cplusplus.com/reference/string/string/
Sep 2, 2010 at 8:31am

Suggestion: Use std::strings instead of (const) char pointers/arrays to hold your strings.


Sometimes we have to deal with legacy C API we got no choice. We maybe writing a new C++ program but we are required to interface with legacy C API that usually accept only char * in the function arguments for string datatype. We can use C++ string c_str() method I know but old habits die hard for me :P

Although I hope for those legacy C API to be obsoleted, the real world is there are still lotsa of legacy code depending on those C API. I guess char * is hard to eliminate in another 10 years I guess :P


Topic archived. No new replies allowed.