Thread: Naming folders after the date - my futile attempt

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    Naming folders after the date - my futile attempt

    guys, i have a problem that i cant name folders after the date we are in.

    in the below code: GPpsTime has been calculated and updates regulary. and "day" is the current number of day of the year. the file creation bit works perfectly as i have tried it elsewhere.

    my attempt below doesnt work because i might be acting silly and assuming that
    Code:
    &day
    will automatically change the name of the new folder.

    can anyone help me?

    my attempt

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <windows.h>
    
    
    //******************************************************************
    
    //Creates a new directory everytime the clock ticks over to 00:00:00.
    
    //Directory is given the name of the current day
    
    //uses names "GpsTime" and "Day" from time conversion routine
    
    //Creates 24 new files within directory with hour names
    
    //Continues onto writing routine
    
    //******************************************************************
    
    #define MAX 24
    
    int main()
    {
    
    int clock, hour, last_hour = -1;
    
    while(1)
    {
    		  unsigned int hour = (GpsTime / 3600) % 24;  //current hour we are in
              if (hour == 0 && last_hour == 11)   //when hour ticks over to 0, create new directory
              {
                   CreateDirectory("c:\\temp\\day", NULL);  //creates a folder with current day as name
    			   printf("Directory created\n");
    
              }
              last_hour = hour;
    
    }
    			SetCurrentDirectory("c:\\temp\\&day");		//sets current directory to day for creation of files
    {
    
    char buf[BUFSIZ];
    
    FILE *fp;
    int i;
    
    for (i=0; i<MAX; i++)
    	{
        sprintf(buf, "%d h.txt", i+1);
        if ((fp = fopen(buf, "a+")) == NULL)
    	{
            fprintf(stderr, "\n\n\n\n\nCan't open %s!\n\n\n\n\n", buf);
            continue;
    	}
    	}
    
    	printf("\n\n\n\n\n\nNew directory and files created, Program will now continue\n\n\n\n");;
    
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, looks like you're mixing say VB's string concatenation with C++

    In C, I'd use sprintf, though some C++ purists would say use strstream
    Code:
    char newdir[1000];
    sprintf( newdir, "c:\\temp\\day%d-hour%d", day, hour );
    CreateDirectory(newdir, NULL);
    You can fiddle with the formatting and content as much as you want to
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    I wrote a program doing the same thing the other day
    ill post the code you can have a look :


    PHP Code:
    #include <iostream>
    #include <fstream>
    #include <time.h>
    #include <string>
    #include <vector>
    #include <process.h>

    using namespace std;

    int main()
    {
        
    struct tm when;
        
    __time64_t now;
        
    _time64( &now );
        
    when = *_localtime64( &now );
        
    vector<stringvDate;
        
    char date;
        
    date asctime( &when );
        
    string sDate;
        
    string sDate3;
        
    sDate date;
        
    sDate3.insert(0sDate010);
        
    sDate3 += ".txt";
        
    fstream file;
        
    file.open(sDate3.c_str(), ios::in);
        if(!
    file.fail())
        {
            
    sDate3.insert(0"type \"");
            
    sDate3.insert(20"\"");
            
    system(sDate3.c_str());
            
    cout << endl << endl;
            
    cout << "Press any key to Continue!" << endl;
            
    cin.get();
        }

        
    char date2;
        
    string sDate2;
        
    string sDate4;
        
    string sInfo;
        
    int quanity;
        
    bool EXIT = false;
        while(EXIT == 
    false)
        {
            
    cout << "Enter Customer Info : " << endl;
            
    getline(cinsInfo'\n');
            
    cout << "Enter Quanity : ";
            
    cin >> quanity;
            
    quanity -= 20;
            if(
    sInfo[0] == '\0')
            {
                EXIT = 
    true;
            }
            else
            {
            
    cin.ignore();
            if(
    quanity 0)
            {
                
    cout << "Quanity Must be more then 20" << endl;
                
    cout << "Re enter the amount : ";
                
    cin >> quanity;
                
    cin.ignore();
            }
                for(
    int i 0!= 3i++)
                {
                    
    when.tm_mday when.tm_mday quanity;
                    
    _mktime64( &when );
                    
    date2 asctime( &when );
                    
    sDate2 date2;
                    
    sDate4.insert(0sDate2010);
                    
    vDate.push_back(sDate4);
                    
    sDate4 += ".txt";
                    
    fstream file1;
                    
    file1.open(sDate4.c_str(), ios::app ios::out);
                    if(
    == 0)
                    {
                        
    file1 << sInfo << endl;
                    }
                    else
                    {
                        
    file1 << endl << "*** ORIGINAL REMIND DATE " << vDate[0] << " ***" << endl;
                        
    file1 << sInfo << endl << endl;
                    }
                    
    file1.close();
                    
    sDate2.clear();
                    
    sDate4.clear();
                }
            }
        }
        return 
    0;


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 08:55 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM