Thread: Date on Filename

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    Date on Filename

    Helo!
    Does anyone know how i put date on filenames.Now i use this code to create a new file:

    FILE *fil1,*fil2;
    fil1=fopen("sample.txt","w");

    i know that time.h has the date,but how do i put this date in the filename?


    Thanks!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You cannot do it the way you have up there. . . it is more along these lines:
    Code:
    char filename[1024];
    
    sprintf(filename, "sample-%i%02i%02i.txt", date.year, date.month, date.day);
    
    fil1 = fopen(filename, "w");

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    8
    Quote Originally Posted by Kennedy View Post
    You cannot do it the way you have up there. . . it is more along these lines:
    Code:
    char filename[1024];
    
    sprintf(filename, "sample-%i%02i%02i.txt", date.year, date.month, date.day);
    
    fil1 = fopen(filename, "w");
    I tried it didn't work i just got some random numbers behind the name,maybe the adress? Here is my code by the way.

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
     int main(void)
     {
        int teller=0;
        int i=0;
        char szBuff[100+1] ={0};
        DWORD dwBytesRead = 0;
        char dateStr [9];
        char timeStr [9];
        int verdi;
         int len;
        _strdate( dateStr);
        _strtime( timeStr );
    
    
    
    
    
    
        //initsialisering av filer
    
        FILE *fil1,*fil2;
        fil1=fopen("sample.txt","w");
        fil2=fopen("sample2.csv", "w");
    
    
    
            //denne første delen inneholder kode for å åpne den serielle porten
    
         HANDLE hSerial;                          // oppretter et HANDLE objekt.Handle er en identifikatorer for objektene kernel er ansvarlig for
    
        hSerial = CreateFile("COM1",             // hSerial som er av typen HANDLE filnavnet"com1"
        GENERIC_READ | GENERIC_WRITE,            //deklarerer om man ønsker å lese fra eller til den seriell porten
        0,                                       //skal altid være 0
        0,
        OPEN_EXISTING,                           // forteller at windows bare skal åpne en eksisterende fil.i vårt tilfelle eksisterer com porten
        FILE_ATTRIBUTE_NORMAL,
        0);
    
        // Her skjekkes om porten eksisterer
        if(hSerial==INVALID_HANDLE_VALUE){
            if(GetLastError()==ERROR_FILE_NOT_FOUND){
                printf("Port finnes ikke");      //serial port does not exist. Inform user.
    
            }
            printf("Ukjent feil");
                                                //some other error occurred. Inform user.
        }
    
    
       /*Her settes egenskapene til den serielle porten baud rate, (stop bits, etc)
        DCB struct definerer egenskapene for en seriell kommunikasjon i windows http://msdn.microsoft.com/en-us/library/aa363214(VS.85).aspx
       */
        DCB dcbSerialParams = {0};              //setter alle felt til 0
        dcbSerialParams.DCBlength=sizeof(dcbSerialParams);  //
    
        if (!GetCommState(hSerial, &dcbSerialParams)) {     //tar Handlen vi opprettet og DCB struct til å fylle de parametrene som er i bruk nå i porten
            printf("error getting state");
        //error getting state
        }
    
        //trenger da bare å sette de parametrene vi har bruk for
        dcbSerialParams.BaudRate=CBR_19200;
        dcbSerialParams.ByteSize=8;
        dcbSerialParams.StopBits=ONESTOPBIT;
        dcbSerialParams.Parity=NOPARITY;
    
        if(!SetCommState(hSerial, &dcbSerialParams)){
            printf("error setting state");
        //error setting serial port state
        }
    
        /*Setting timeouts: Dersom det ikke kommer noen data inn på den serielle porten
        og man prøver å lese fra den kan aplikasjonen låse seg mens den venter på data.
        Enkleste måten er å sette en ventetid
        *ReadIntervalTimeout:Hvor lenge i millisekund det skal ventes mellom vær karakter
        *ReadTotalTimeoutant:ventetid før returnering
        *ReadTotalTimeoutMultiplier: Spesifiserer ventetid før returnering for hver byte som skulle lese
        *WriteTotalTimeoutConstant og WriteTotalTimeoutMultiplier: leser
        */
        COMMTIMEOUTS timeouts={0};
        timeouts.ReadIntervalTimeout=50;
        timeouts.ReadTotalTimeoutConstant=50;
        timeouts.ReadTotalTimeoutMultiplier=10;
    
        timeouts.WriteTotalTimeoutConstant=50;
        timeouts.WriteTotalTimeoutMultiplier=10;
        if(!SetCommTimeouts(hSerial, &timeouts)){
        //error occureed. Inform user
        }
    
        // lesing og skriving av data
    
        fprintf(fil1,"vannivået er:\n");
    
          while(1){
    
            if(!ReadFile(hSerial,szBuff,100, &dwBytesRead, NULL)){
                    printf("ikke motatt");
            //error occurred. Report to user.
            }
    
    
    
    
            verdi=atoi(szBuff);
            len = strlen(szBuff);
    
            if(len==3){
    
                printf(szBuff);
                printf("\n");
    
                if(verdi<209){
                    printf("tank tom");
                      verdi=0;
    
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
                }
    
                if((verdi>=212) && (verdi<=222)){
    
                        verdi=5;
                      printf("5cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
                }
    
                 if((verdi>=232) && (verdi<=242)){
                        verdi=10;
                        printf("10cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
    
                }
    
    
                if((verdi>=248) && (verdi<=265)){
                        verdi=15;
                        printf("15cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
    
                }
    
                if((verdi>=273) && (verdi<=283)){
                        verdi=20;
                        printf("20cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
                }
    
                 if((verdi>=294) && (verdi<=304)){
                        verdi=25;
                        printf("25cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
    
                }
    
    
    
                if((verdi>=312) && (verdi<=322)){
                        verdi=30;
                        printf("30cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
    
                }
    
                 if((verdi>=337) && (verdi<=347)){
                        verdi=35;
                        printf("35cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
    
                }
    
    
                if((verdi>=355) && (verdi<=365)){
                        verdi=40;
                        printf("40cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
    
                }
    
                 if((verdi>=373) && (verdi<=383)){
                        verdi=45;
                        printf("45cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
                }
    
    
                if((verdi>=394) && (verdi<=404)){
                        verdi=50;
                        printf("50cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
                }
    
                 if((verdi>=416) && (verdi<=426)){
                        verdi=55;
                        printf("55cm");
                     fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
                }
    
                if((verdi>=437) && (verdi<=447)){
                        verdi=60;
                        printf("60cm");
                      fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
                }
    
                 if((verdi>=457) && (verdi<=467)){
                        verdi=65;
                        printf("65cm");
                     fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
    
                }
    
                 if((verdi>=478) && (verdi<=488)){
                        verdi=70;
                        printf("70cm");
                     fprintf(fil1,"%dcm",verdi);
                      fprintf(fil1,"                      %s,", dateStr);
                      fprintf(fil1,"%s\n", timeStr);
    
                      fprintf(fil2,"%d",verdi);
                      fprintf(fil2,"\n");
                      fprintf(fil1,"\n");
    
    
                }
            printf("\n");
            teller++;
            }
    
    
    
    
           }
            fclose(fil1);
             fclose(fil2);
    
    
        // NB viktig å lukke Handlen vi opprettet etter bruk
        CloseHandle(hSerial);
    
    
        char lastError[1024];
        FormatMessage(
        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        lastError,
        1024,
        NULL);
    
    
    }
    Last edited by zyberb; 05-06-2010 at 02:10 PM.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Um, that was not _real_ code from my post.

    1) get the date (in whatever format) and verify that you get the date via printing the date to the screen.

    2) parse the date (if necessary) -- or ask for the date in a structure.

    3) Now you know where to access year, month, and day. SO, then you do this:
    Code:
    char filename[1024];
    sprintf(filename, "%s-%i%02i%02i%s",<filename start>, <year>, <month>, <day>, <any extension you want>);
    file = fopen(filename, "w");

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A good place to start looking for time and date information is something like this: ctime (time.h) - C++ Reference

    Also, that code looks highly repetitive. You can almost certainly make it easier to read and understand by breaking out some of the code into functions or performing some calculations.

    Food for thought: you could create an array holding the boundary values, like
    Code:
    const int boundary[] = {
        209, 212, 222, 242, 265, /* ... */
    };
    Then you could check which index the verdi falls into -- say it's index x, that means you need to print x*5 cm (just handle zero as a special case). If you do something like that then your code will be much more compact.

    [I'm assuming here that you wanted to partition the integers into these separate ranges -- if there are some numbers you don't want to deal with at all, then you can handle that as well.]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. libcurl experience and question
    By Dino in forum C Programming
    Replies: 6
    Last Post: 10-26-2009, 02:07 PM
  2. 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
  3. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  4. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 08:35 AM