Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Creating a file named Time and Date problems

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello
i tried to make a file named time and date but it does not create...
My guess is that name gets too long, But could someone fix this one for me... or tell me where i go wrong?

void Error::DisplayError(std::string WhatsError)
{
time_t Timer = time(0);
tm* LocalTime = localtime(&Timer);
std::string TimeName = asctime(LocalTime);
TimeName += ".txt";
std::ofstream OutStream(TimeName.c_str(), std::ios::out | std::ios::app);
if(OutStream != NULL)
{
OutStream << WhatsError << "\n";
}
else
{
//std::ofstream AStream("error.txt",)
}
}


The text file is not created

Share this post


Link to post
Share on other sites
Advertisement
You probably don't want to create a file for every error. It would be easier to have a log for every day, or week, or session, depending on the style of the program. The period chosen should reflect the approximate frequency you might expect such errors to occur in. If your files are too small, then managing the files becomes a nightmare. If the files are too large, managing the contents becomes a nightmare.

Consider:

  • Some tools do not handle large files gracefully
  • If you want your user to send you the error logs

    • You want to avoid requiring them to package thousands of tiny log files
    • You want to avoid forcing them to manage a massive, multi-gigabyte log


      If you prefix each log message with the current timestamp, you can still easily grep the files for errors on a given day or time.

Share this post


Link to post
Share on other sites


std::string TimeName = asctime(LocalTime);


Adds a \x0A (new line) at the end.

Would be nice if you put more info.
EX what you say is "std::string TimeName = asctime(LocalTime)\x0A;"
what does \x0A do.

Share this post


Link to post
Share on other sites

You probably don't want to create a file for every error. It would be easier to have a log for every day, or week, or session, depending on the style of the program. The period chosen should reflect the approximate frequency you might expect such errors to occur in. If your files are too small, then managing the files becomes a nightmare. If the files are too large, managing the contents becomes a nightmare.

Consider:

  • Some tools do not handle large files gracefully
  • If you want your user to send you the error logs

    • You want to avoid requiring them to package thousands of tiny log files
    • You want to avoid forcing them to manage a massive, multi-gigabyte log


      If you prefix each log message with the current timestamp, you can still easily grep the files for errors on a given day or time.

Before i trouble my self with that kind of stuff, First i need to get file created with Current month/day
And yes i was considering to do that later.
Thanks on the tip very helpful.

Share this post


Link to post
Share on other sites

Would be nice if you put more info.
EX what you say is "std::string TimeName = asctime(LocalTime)\x0A;"
what does \x0A do.

he said it is a new line alias "\n". Your path has a line break in it and im guessing ofstream doesn't know what to do with an input string with line break. It aborts, returning null and " OutStream << WhatsError << "\n" " is never called.

Share this post


Link to post
Share on other sites

[quote name='BaneTrapper' timestamp='1345460227' post='4971412']
Would be nice if you put more info.
EX what you say is "std::string TimeName = asctime(LocalTime)\x0A;"
what does \x0A do.

he said it is a new line alias "\n". Your path has a line break in it and im guessing ofstream doesn't know what to do with an input string with line break. It aborts, returning null and " OutStream << WhatsError << "\n" " is never called.
[/quote]
I figured that out just now. Weird thing they do there.
Well that fixes my error Thanks for help.

Share this post


Link to post
Share on other sites

BTW in General it makes more sense to put FileIO (or IO in general) in a try/catch/finally clause rather than checking for != null.

...and this is not really tip-top advice, since C++ does not have a finally clause and because converting IOStreams to use exceptions is convoluted and rarley done (because it's particularly exceptional to encounter I/O problems), it's generally considered more idiomatic to use the implicit conversion to [font=courier new,courier,monospace]bool[/font] when checking an IOStream for an open failure. Edited by Bregma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!