Create text file with the name as System date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshnarra
    New Member
    • Dec 2010
    • 8

    #1

    Create text file with the name as System date

    Hi,

    I want to create a text file with the name as SYSTEM DATE...
    for example---28/04/2008:9:11:50.tx t
    The File name should be allocated dynamically.

    Can you provide me some details how to write the code for this..

    Plese help me.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The first thing you want to do is confirm that a filename of that form is legal for the operating systems that you intend to run your program on.

    Step 1: obtain the system date and time in the desired format.

    Step 2: create a file with that filename.

    Which step are you having trouble with?

    Comment

    • maheshnarra
      New Member
      • Dec 2010
      • 8

      #3
      hi,
      thanks for the reply.
      i cant able to understand how to create a file with the file name.
      i tried this code.

      #include <time.h>
      #include<stdio. h>
      #include<sys/types.h>
      #include<sys/types.h>
      #include<fcntl. h>
      int main()
      {
      char s[30];
      int fp;
      size_t i;
      struct tm tim;
      time_t now;
      now = time(NULL);
      tim = *(localtime(&no w));
      i = strftime(s,30," %b %d, %Y; %H:%M:%S\n",&ti m);
      printf("%s\n",s );
      }
      output of this is
      Dec 21, 2010; 10:30:34

      i want to open a file like this :
      Dec 21, 2010; 10:30:34.txt

      Please give me suggestion regarding this.Is it possible to open a file like this?

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        For Some os it is not possible. as Example Windows. You cannot add : to a file name. Cause it is a special character that separates directory from file name

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          I suggest you use strftime format string "%Y-%m-%dT%H-%M-%S.txt". This avoids troublesome characters (such as ":" and space). It also has the added benefit that an alphabetical listing (such as when you list the contents of a directory) appear in chronological order.

          It isn't obvious to me why you terminated your format string with newline.

          (I'm suggesting a variant of the W3C DTF standard for date/time strings -- replace colons with dashes and omit the time zone designator.)

          By the way, you should confirm that the return code from strftime is nonzero.

          Comment

          • maheshnarra
            New Member
            • Dec 2010
            • 8

            #6
            Hi,
            Thanks for your suggestions.I kept the
            strftime format string "%Y-%m-%d T%H-%M-%S.txt" and tried.The output is 2010-12-22T10-20-31.txt
            but the text file is not opening in the directory.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              How are you trying to open the text file?

              Comment

              • maheshnarra
                New Member
                • Dec 2010
                • 8

                #8
                I opened a file like this `date +%d-%m-%Y`.txt
                I can able to create the file successfully.
                Actually i want to append the data from mouse into this file.I got the data from mouse using libusb API and i can print that data.

                Please help me how to append the data from mouse into file (20-10-2020.txt).Is it possible to append data into the file if we create a file like this..

                Please give me some suggestions regarding this.

                thanku

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Please show us the C code that opens the file.

                  Comment

                  • maheshnarra
                    New Member
                    • Dec 2010
                    • 8

                    #10
                    thanks for your suggestions.I can able to open the file.I formatted the fopen incorrectly.Now i got it.

                    Comment

                    Working...