Member Avatar for Geek-Master

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.

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");
}
Member Avatar for Geek-Master

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.