So i am trying to make a program that takes the date from windows (this is not where i have the problem). My problem is i want to use that date and create a txt file for each day. For example it would create a file named... 8_4_2010 for today and then 8_5_2010 for tomorrow.
Here is the code i have...

//this successfully gets the date i have check it
char dateStr [9];
_strdate( dateStr);

//this sets the name for the file
ofstream out(_strdate.c_str());
out.open(_strdate.c_str(),ios::out |ios::app);
out.flush();
out.close();

I get the error message...
request for member `c_str' in `_strdate', which is of non-class type `char*()(char*)'

i used the c_str because i had the error message...
no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(char*(&)(char*))'

and when i reseached that error message the c_str was supposed the solve the problem. there probably is something wrong with my approach to what i am trying to accomplish so i was just looking for some input.

oh and this is my first time posting so i hope i followed all of the rules of posting if not just tell me so i can do it differently next time.

_strdate is a function, so calling c_str() won't help any.

This might work:

ofstream out(_strdate(dateStr));

or maybe this will do:

ofstream out(dateStr);

Be sure that 09/12/11 is a valid file name though. I'm not sure that it is.

thank you the error message is now gone but i still have the problem that the filename is just "dateStr.txt" and not the actual date. does anyone have any ideas how this could work? thank you in advance

The way I do it is to first call time() and localtime() to get the data as struct tm. Then use sprintf() to format the date into a character array

char date[40];
time_t now = time(0);
struct tm* tm = localtime(&now);
// now format the date -- I put it in yyyymmdd format so that the files
// can be easily sorted.
sprintf(date,"%04d%02d%02d", tm->tm_year+1900,tm->tm_mon+1, tm->tm_mday);
ofstream out(date);

That means that you are somehow specifying the file name as "dateStr.txt" rather than using the contents of the variable dateStr.

Perhaps this function will help.
Maybe try something like this?:

#include <ctime>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
  const int MAX_CHARS = 25;
  time_t timestamp = time(0); //create a time object and get a timestamp
  char fileName[MAX_CHARS]; //create a file name buffer

  cout << "The time is: " << timeObject << endl; //display the timestamp
  strftime(fileName, MAX_CHARS, "%m_%d_%y.txt\0", localtime(&timeObject));
  //generate the file name from the timestamp, follow the link I gave you

  cout << "The file name is: " << fileName << endl;  //display generated file name

  ofstream datedFile(fileName, ios::out | ios::app); //open the file stream

  datedFile << fileName << "\n";
  datedFile << "This should be on a new line.";  //write some data to the stream

  cin.get();
  return 0;
}

EDIT:
oops, looks like AD beat me to it....

this worked great! thank you so much. the filename is the date.

the only thing that is slightly off now is that it is not creating the file as a txt file now. it is just a nondescript file type.

To fix that, you have 2 options:

  1. Just add ".txt" to the definition of the format string as I did in my version (see Line 13).
  2. Perform a concatenation after you generate the desired file name using strncat().

huge help guys thanks. its solved.

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.