0

I've been playing around with files and streams and just hit a pot hole.

My goal is to be able to input time and date into a file when say someone "clocks in/out". Just a little program that will help me keep up with my time in school.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream a_file ( "example.txt", ios::app);
    if ( !a_file.is_open() ) {
        // The file could not be opened
    }
    else {
        // Safely use the file stream
    }
    a_file << system("date /t") << "\n" << system("time /t") << "\n";
    a_file. close();
}

The problem is that when I open "example.txt" all I get is a zero (0).

I think my problem has to do with I'm inputing an integer instead of a string, since I'm working with a text file. If anyone has some input, I'd enjoy hearing from you.

2
Contributors
2
Replies
3
Views
12 Years
Discussion Span
Last Post by Geek-Master
0

system returns an implementation defined value, if it returns at all. Try something along these lines instead:

#include <cstdlib>

int main()
{
  std::system("date /t > example.txt");
  std::system("time /t >> example.txt");
}
0

your way is a lot better. That way I can make that a function and call for it when I need it. Cool, thanks a lot Narue.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.