# Crashes while writing to file

This topic is 2178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello

The problem im having is that the program crashes while writing to a file.

I have made a tool that generates code during the pre build step.

The code doing the writing looks like this:

#define WRITE_LINE( x ) file << x << "\n";

std::ofstream file( "GenClass.cpp" );
...
WRITE_LINE( "some text" << someVariable << "some more text" )
WRITE_LINE( "long line of text" )


The code is very simple.
And at the current state it dosnt write that much either.
It writes about 1000 lines of code.

It crashes when it tries to write regular text just like the second example of WRITE_LINE.

If for example the text im trying to write at the point of the crash is "void TestFunction()".

It only writes "void Tes".

If i change the text to "//void TestFunction()".

It only writes "//void T".

So it feels like it maxes out some sort of buffer.

Never run into this before, so not quite sure what to do.

##### Share on other sites
Can you try to boil down your program to the smallest possible program that still shows the problem, and then post it? Edited by Álvaro

##### Share on other sites

That will be really hard but ill give it a try.

I also utilize lambdas like this if it can affect the stream somehow:

auto WriteSectionComment = [&file]( const std::string & _Title )
{
WRITE_LINE( "/////////////////////////////////////////////////" )
WRITE_LINE( "// " << _Title )
WRITE_LINE( "/////////////////////////////////////////////////" )
WRITE_LINE( "" )
};


##### Share on other sites

Ah dang, thanks for the reply.
While i was trying to break it down it just poped out.

I forgot to check for a null pointer.

Damn... Been looking over the code so many times and overlooked it every single time.

Thanks!
It now works properly.

Edited by Noxil

##### Share on other sites

Ah dang, thanks for the reply.
While i was trying to break it down it just poped out.

Actually, that's what usually happens when trying to simplify a buggy program into the minimal program that shows the bug. And if it doesn't pop up, you can always take the result and post it on a forum, with much better chances of getting relevant help.

• 18
• 18
• 11
• 21
• 16