We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,039 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Creating text file with variable name in directory

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

5
Contributors
7
Replies
1 Day
Discussion Span
8 Months Ago
Last Updated
9
Views
Question
Answered
AutoPython
Junior Poster
142 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
Skill Endorsements: 0

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));
}
rubberman
Posting Maven
2,572 posts since Mar 2010
Reputation Points: 365
Solved Threads: 306
Skill Endorsements: 52

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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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;
}
AutoPython
Junior Poster
142 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
Skill Endorsements: 0

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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
vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11

Not sure why the above suggestions were sprintf :S

string VariableString = "gsdgdsghas";

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

ofstream write(FilePath.c_str());
triumphost
Practically a Master Poster
625 posts since Oct 2009
Reputation Points: 59
Solved Threads: 55
Skill Endorsements: 1
Question Answered as of 8 Months Ago by WaltP, vijayan121, triumphost and 1 other

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

AutoPython
Junior Poster
142 posts since Sep 2009
Reputation Points: 14
Solved Threads: 18
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0980 seconds using 2.7MB