Time in C++

This is a discussion on Time in C++ within the Linux Programming forums, part of the Platform Specific Boards category; I'm trying to get the time in months and days, but I've only found codes in C to do that. ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    45

    Time in C++

    I'm trying to get the time in months and days, but I've only found codes in C to do that.
    Actually, the intention of this is to tar a file, and record the month and day somewhere, ideally in the name, but a hidden file would also work.

    Here's the program as it is now, no time function.
    Code:
    #include <iostream>
    #include <stdlib.h>  //For system()
    
    using namespace std;
    
    int main()
    {
    
    //Creates a hidden tarball in home.
    system("tar -cvvf ~/.Record.tar Record/");
    
    }
    If I wanted to make a variable go in the name, would it be done as system("tar -cvvf ~/."<< x <<"Record.tar Record/"); ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    C++ appropriated most of the C standard library, and that was part of it. So change <time.h> to <ctime> and off you go.

    Notice that system requires a C-string, so you would have to build your command somewhere (a stringstream perhaps) and then convert it (with .str().c_str() or whatever)

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    45
    I don't quite understand the second part.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You want to do system(something) right? That something can't be a std::string, it has to be a C-string. But presumably you want to build it using all the power tools C++ gives you. So:
    Code:
    std::stringstream command;
    command << "tar -cvvf ~/.";
    command << month << day << "Record.tar Record/";
    system(command.str().c_str());
    You have to use str to get a string back from your stringstream object, and then use c_str on that to get a C-string you can pass to system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 01:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21