Jump to content
• Advertisement

Public Group

# Creating a file named Time and Date problems

This topic is 2310 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

##### Share on other sites
Advertisement
 std::string TimeName = asctime(LocalTime); 

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

#### Share this 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.

#### Share this 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.

#### Share this 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

##### 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

##### 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

##### 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

##### 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5
• Advertisement

• 9
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013665
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!