filename

hi,
i do create a menue function that should generate separate .txt files for users. commands like ofstream are fine but i have to predefine the filename. however, i would like to name the file according to the user entry...

void Cfile ()
{
string fileName;
cout << ("Please enter the Name of your library...");
getline (cin, fileName);
ofstream name (filename.txt); // ?? thats the problematic line...
name << "My first trial to save information \n";
name.close();}

guess its easy but somehow i am not able to find a solution. thanks for your help,
cheers...
unfortunately streams don't use string's. You need the c string like so:
ofstream name (filename.c_str());


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
On Windows you could use the GetUserName API function. On Linux there's sure a similar API call available.

1
2
3
4
BOOL WINAPI GetUserName(
  __out    LPTSTR lpBuffer,
  __inout  LPDWORD lpnSize
);

Source: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724432%28v=vs.85%29.aspx

Example:
1
2
3
char name[255];
GetUserName(name, 255);
cout << "Your session name is: " << name;

Missing by this example: error checking :-P
Last edited on
great txh,
cheers...
Topic archived. No new replies allowed.