[qoute]
If you have a folder called Data in your C drive, in Data folder there are day_01.txt, day_02.txt ,……………………day_30.txt . you need to build a program that called the file according to it previous date, so if the date 2/10/2005 you have to call and open file day_01.txt.

Notice1: the month affect the number of the txt file in the Data folder.
Notice2: each txt file just have one line which contains the date of the creation of that file. e.g
day_01.txt have a one line of 01/10/2005.

Write a program with respect to the above.
I did not answer it :cry:
[/qoute]

why didn't you answer it? Its not that difficult. If you know what today is, then just use sprintf() to format the filename

int day_of_month = 10;
char filename[255];

sprintf(filename,"day_%02d.txt", day_of_month);

*Edit) __asm__("movl %eax, %eax") ;

[Qoute]
when the question mention the example of 2/10/2005 it just an example, what I was thinking is to get the current date then subtracted from the previous date, then call the file, I have the idea and the lecture said it right, but I do not know why I am not sure how to write the code for it.[/qoute]

use functions in time.h -- today() returns the current date as an integer, number of seconds since 1 Jan 1970 (I think). Then call localtime() which returns pointer to struct tm. That structor contains individual fields for month,day,year, hour, ... You need to know how to use pointers and structures to make use of that information. If you don't, then study up on them in your programming book.

use functions in time.h -- today() returns the current date as an integer, number of seconds since 1 Jan 1970 (I think).

Do you mean time (which returns a time_t , by the way)?

yes Dave, sorry for the error.

[qoute]
Hi
I am trying to use the code below, but I am not sure why it is not working, it gives me error.[/qoute]

#include<stdio.h>

void printdate();
time_t yesterday();


int main() 
{
    time_t y = yesterday();
	printdate(y);
  return 0;
}


/*********** function to print date on screen********/

void printdate(time_t d) 
{
  struct tm*   t;
  char buf[100];

  t=localtime(&d);

  strftime(buf, sizeof(buf), "Yesterday was: %Y-%m-%d\n", t);

  printf(buf);
}

/*************** Function to get Yesterday***************/

time_t yesterday()
 {
  time_t today = time(NULL);
  return today - (time_t)(24*60*60);
}

what and where is the error? You need to include time.h like you did stdio.h

#include <time.h>

And match the function defintions to the prototypes.

Note that proper handling of date/time stuff is much more complicated.
http://www.daniweb.com/code/snippet263.html

And please spell quote correctly, or just don't use it -- especially since you are apparently not quoting anybody.

[qoute]
Hi
and thanks for your relpy, the error is: error C2660: 'printdate' : function does not take 1 parameters, inspite of I define it to take one parameters.
it still giving me the same error even when I added #include<time.h>.
I am not sure what is my mistake.
[/qoute]

what and where is the error? You need to include time.h like you did stdio.h

[qoute]
Hi
and thanks for your relpy, the error is: error C2660: 'printdate' : function does not take 1 parameters, inspite of I define it to take one parameters.
it still giving me the same error even when I added #include<time.h>.
I am not sure what is my mistake.
[/qoute]

At the top of the program you prototyped printdate() without any parameters. In c++ (unlike C) that means it takes no parameters. Correct the error in the protytype

void printdate(time_t d) ;

and read Dave's post about quotes!

Hi
thank you for your help.
I wonder if it possible to use it to use the code .

sprintf(filename,"day_%02d.txt", y);

to get the file day_26.txt from the data folder.

thanks for the advise on using the qoute.

Why not just specify that format in strftime ?

Hi Dave
I am not quiet sure how it can benefit me, my code to get yesterday date is:

#include <stdio.h>
#include <time.h>

void printdate(time_t d);
time_t yesterday();


int main() 
{

	char filename[255];
    
	time_t y = yesterday();
	printdate(y);
	sprintf(filename,"day_%02d.txt", y);
  return 0;
}


/*********** function to print date on screen********/

void printdate(time_t d) 
{
  struct tm*   t;
  char buf[100];

  t=localtime(&d);

  strftime(buf, sizeof(buf), "Yesterday was: %d/%m/%Y\n", t);

  printf(buf);
}

/*************** Function to get Yesterday***************/

time_t yesterday()
 {
  time_t today = time(NULL);
  return today - (time_t)(24*60*60);
}

so now I would like to use the yesterday date to open the file day_26.txt in data folder. so I was thinking to use sprintf according to help of the previous posts.

#include <stdio.h>
#include <time.h>

char *foo(int days, const char *format, char *text, size_t size)
{
   time_t t;
   if ( time ( &t ) != (time_t)(-1) )
   {
      struct tm *local = localtime ( &t );
      if ( local )
      {
         local->tm_mday += days;
         t = mktime ( local );
         if ( t != (time_t)(-1) )
         {
            local = localtime ( &t );
            if ( local )
            {
               if ( strftime ( text, size, format, local ) )
               {
                  return text;
               }
            }
         }
      }
   }
   return 0;
}

int main(void)
{
   char filename[20];
   if ( foo(-1, "day_%d.txt", filename, sizeof filename) )
   {
      printf("filename = \"%s\"\n", filename);
   }
   return 0;
}

/* my output
filename = "day_26.txt"
*/

[edit]

#include <stdio.h>
#include <time.h>

char *foo(int days, const char *format, char *text, size_t size);

int main(void)
{
   char filename[20];
   int i;
   for (i = -10; i < 0; ++i)
   {
      if ( foo(i, "day_%d.txt", filename, sizeof filename) )
      {
         printf("filename = \"%s\"\n", filename);
      }
   }
   return 0;
}

char *foo(int days, const char *format, char *text, size_t size)
{
   time_t t;
   if ( time ( &t ) != (time_t)(-1) )
   {
      struct tm *local = localtime ( &t );
      if ( local )
      {
         local->tm_mday += days;
         t = mktime ( local );
         if ( t != (time_t)(-1) )
         {
            local = localtime ( &t );
            if ( local )
            {
               if ( strftime ( text, size, format, local ) )
               {
                  return text;
               }
            }
         }
      }
   }
   return 0;
}

/* my output
filename = "day_17.txt"
filename = "day_18.txt"
filename = "day_19.txt"
filename = "day_20.txt"
filename = "day_21.txt"
filename = "day_22.txt"
filename = "day_23.txt"
filename = "day_24.txt"
filename = "day_25.txt"
filename = "day_26.txt"
*/

Hi Dave
many thanks for you help, it helped me so much, A question pops up to my mind is the code going be affect if the day is 31,30,28,29.

I'd say keep looking at the Time/Date Calculations code snippet for a while, and ask questions if there are parts you don't understand. Calendar dates have some very strange-seeming handling.

But let's say that today is Jan 5, 2006 and we want the previous 10 days' files:

#include <stdio.h>
#include <time.h>

char *foo(int days, const char *format, char *text, size_t size, time_t t)
{
   struct tm *local = localtime ( &t );
   if ( local )
   {
      local->tm_mday += days;
      t = mktime ( local );
      if ( t != (time_t)(-1) )
      {
         local = localtime ( &t );
         if ( local )
         {
            if ( strftime ( text, size, format, local ) )
            {
               return text;
            }
         }
      }
   }
   return 0;
}

int main(void)
{
   char filename[20];
   int i;
   time_t t;
   struct tm date = {0};
   date.tm_mon = 0;    /* January */
   date.tm_mday = 5;   /* 5th */
   date.tm_year = 106; /* 2006 */
   t = mktime ( &date );
   if ( t != (time_t)(-1) )
   {
      fputs(ctime(&t), stdout);
      for ( i = -1; i >= -10; --i )
      {
         if ( foo(i, "day_%d.txt", filename, sizeof filename, t) )
         {
            printf("%3d filename = \"%s\"\n", i, filename);
         }
      }
   }
   return 0;
}

/* my output
Thu Jan 05 00:00:00 2006
 -1 filename = "day_04.txt"
 -2 filename = "day_03.txt"
 -3 filename = "day_02.txt"
 -4 filename = "day_01.txt"
 -5 filename = "day_31.txt"
 -6 filename = "day_30.txt"
 -7 filename = "day_29.txt"
 -8 filename = "day_28.txt"
 -9 filename = "day_27.txt"
-10 filename = "day_26.txt"
*/

Hi Dave
many thanks for you help, I understand the code very well. on last thing How do I mark this post as solved.

Hi Dave
I was looking on this code.

#include <stdio.h>
#include <time.h>

char *foo(int days, const char *format, char *text, size_t size)
{
   time_t t;
   if ( time ( &t ) != (time_t)(-1) )
   {
      struct tm *local = localtime ( &t );
      if ( local )
      {
         local->tm_mday += days;
         t = mktime ( local );
         if ( t != (time_t)(-1) )
         {
            local = localtime ( &t );
            if ( local )
            {
               if ( strftime ( text, size, format, local ) )
               {
                  return text;
               }
            }
         }
      }
   }
   return 0;
}

int main(void)
{
   char filename[20];
   if ( foo(-1, "day_%d.txt", filename, sizeof filename) )
   {
      printf("filename = \"%s\"\n", filename);
   }
   return 0;
}

/* my output
filename = "day_01.txt"
*/

can we use the filename and make a condition for it., like

time( &long_time );              
newtime = localtime(&long_time );  
day = newtime->tm_mday;				
month= newtime ->tm_mon;
year=newtime ->tm_year;

	if ( filename == "day_01.txt")
			{
				ofstream out("myfile(month)_(year).csv");
				writestuff(out); 
				out.close();
				cout << "1";
			}
		else 
			{
				ofstream out("myfile(month)_(year).csv");
				writestuff(out); 
				out.close();
				cout << "2";
			}

is it possible to make a new file whenever the filename is day_01.txt or otherwise it is going to open the same file to write in it.

thanks for everyone in advance

month= newtime ->tm_mon +1;
year=newtime ->tm_year + 1900;

don't forget the above adjustments!

Hi
thanks ancient dragon for remind me with that, the problem I am facing is in this condition, which I suppose is right.

if ( filename == "day_01.txt")

it suppose to print

cout << "1";

but actually it gives

cout << "2";

so it should have something wrong but I am not sure what is it.

> if ( filename == "day_01.txt")
Use if ( strcmp( filename, "day_01.txt" ) == 0 )

commented: Hello Salem! +1

Hi
thank you salem. I have a nother question.

month= newtime ->tm_mon +1;
year=newtime ->tm_year + 1900;
ofstream out("myfile(month)_(year).txt");

the out put file, I like to carry month like 12 and year2005, but it actually gives me
the out put file like this myfile(month)_(year).txt, why it is wrong I pass in it the value of the month and the year.

That's already been answered in this thread -- just read some of the earlier posts (#13 and #14). Use sprintf() strftime to format the string.

That's already been answered in this thread -- just read some of the earlier posts (#13 and #14). Use sprintf() strftime to format the string.

Hi
thanks I forget about sprintf(), I did it thanks again. :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.