Hey! I've recently come back to C++ to tackle another project, but I've run into a problem I can't figure out.

#include <iostream>
#include <string>
#include <fstream>
#include <direct.h>

using namespace std;

int main()
{
    string variable = "AwesomeName";

    mkdir ("test");

    ofstream write ("test\\" (variable+".txt").c_str()  ); // this the place where I'm having trouble

    write.close();



return 0;
}

I don't know if this is possible or not, but I want to know if there is a way to create a text file inside a directory using a variable as a name. Any help would be appreciated!

-AP

Not quite. You need to call the ofstream constructor with the name, which can be a variable. Here is the ofstream() constructor's signature:

 ofstream ( const char * filename, ios_base::openmode mode = ios_base::out );

So, as long as you construct the stream with a const char* file name, you are golden. Then, you can write to the ofstream object. Example:

void createAndWriteToFile( const char* vname, const char* somedata )
{
    char fname[MAX_FILE_NAME_LEN];

    // Construct file name here.
    sprintf(fname, "test/%s", vname);

    ofstream ostrm(fname);

    ostrm.write(somedata, strlen(somedata));
}

Look more closely at ("test\" (variable+".txt").c_str() ).
You are missing something.

It's usually better to create complex strings in pieces rather than all in one line like this. Once you get the string generated properly, then start combining the terms.

I pretty much get what was posted (need to look up sprintf), but the code below gives me the error that "fname, MAX_FILE_NAME_LENGTH, and strlen" are not declared in this scope, is there some #include I'm missing?

#include <iostream>
#include <string>
#include <fstream>
#include <direct.h>

using namespace std;

void createAndWriteToFile( const char* vname, const char* somedata )
{
    char fname[MAX_FILE_NAME_LEN];
    // Construct file name here.
    sprintf(fname, "test/%s", vname);
    ofstream ostrm(fname);
    ostrm.write(somedata, strlen(somedata));
    ostrm.close();
}



int main ()
{

    mkdir ("test");
    createAndWriteToFile( "", "Ab");
    return 0;
}

You need the header cstring for the C style function. string is for the C++ string object.

I pretty much get what was posted (need to look up sprintf), but the code below gives me the error that "fname, MAX_FILE_NAME_LENGTH, and strlen"

Why have you started messing around with all this stuff? Your origininal idea was far superior; just that you made a careless typo. Fix that and move on to the next task at hand.

// ofstream write ("test\\" (variable+".txt").c_str()  ); 
// this the place where I'm having trouble

ofstream write( ( "test\\" + variable + ".txt" ).c_str() ) ; 
// you wouldn't have any trouble now

Not sure why the above suggestions were sprintf :S

string VariableString = "gsdgdsghas";

string FilePath = "test/" + VariableString + ".txt";

ofstream write(FilePath.c_str());

Thanks vijayan, fixed the problem. I wasn't thinking of string concatenation when I was typing that line out.

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.