User Input to file name

This might be a dumb question but...I was trying to get a program that I am making as a side project to take user input for the file name and I am stuck something fierce.

This is the line of code I am using to generate the file:

ofstream a_file ("Information.txt", ios::trunc);

and where it says Information I wanted it to replace that with the user input I took for the variable "consumer" but for the life of me I can't figure out how to do it. Any help would be much appreciated!
closed account (zb0S216C)
I'm finding it hard to understand what you mean. Are you saying that you want to replace "Information.txt" with a user-defined string? If so, this is easily accomplished:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <fstream>
#include <iostream>

int main( )
{
    std::string Input;
    std::cin >> Input;

    std::ofstream Out_File( Input.c_str( ), std::ios::truc );

    //...

    return( 0 );
}


Wazzak
Last edited on
Basically I have a program that is collecting information from users and it is being sent out to a text file. I wanted the customer number being put in to be the file name (ie "560.txt" or "76980.txt")
Topic archived. No new replies allowed.