944,139 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13564
  • C++ RSS
Feb 13th, 2005
0

inputing system("date /t") and system("time /t") into a file

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ofstream a_file ( "example.txt", ios::app);
  9. if ( !a_file.is_open() ) {
  10. // The file could not be opened
  11. }
  12. else {
  13. // Safely use the file stream
  14. }
  15. a_file << system("date /t") << "\n" << system("time /t") << "\n";
  16. a_file. close();
  17. }


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.
Similar Threads
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Feb 13th, 2005
0

Re: inputing system("date /t") and system("time /t") into a file

system returns an implementation defined value, if it returns at all. Try something along these lines instead:
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2.  
  3. int main()
  4. {
  5. std::system("date /t > example.txt");
  6. std::system("time /t >> example.txt");
  7. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 13th, 2005
0

Re: inputing system("date /t") and system("time /t") into a file

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.
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Absolute Newbie
Next Thread in C++ Forum Timeline: String operatios





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC