create user-defined-filename file

F

fooxianyang

I'm a newbie in C++ and I'm cracking my head on this.
My lecturer want us to create a file with it's name define by user
itself

example :

program ask what file name the user would like to save ( juz like how
you save a new document in M.office)
ie, the user key in : abc.txt
thus a file name : abc.txt is created and retained for futher usage.

anybody have a solution to it?
It would be great!!!!!
A Million thanks in foware
 
J

Jim Langston

I'm a newbie in C++ and I'm cracking my head on this.
My lecturer want us to create a file with it's name define by user
itself

example :

program ask what file name the user would like to save ( juz like how
you save a new document in M.office)
ie, the user key in : abc.txt
thus a file name : abc.txt is created and retained for futher usage.

anybody have a solution to it?
It would be great!!!!!
A Million thanks in foware

What have you tried so far? Are you familiar with std::getline?
std::string? std::eek:fstream ?
 
F

fooxianyang

Jim said:
What have you tried so far? Are you familiar with std::getline?
std::string? std::eek:fstream ?

I've tried both... but it just wont compile.
nonetheless, i found a solution on other forum :

ofstream outFile( variable_file_name.c_str());

the above could created the file with variable value as name ( in this
case, variable_file_name)
but i'm not quite clear of the concept
 
J

Jim Langston

I've tried both... but it just wont compile.
nonetheless, i found a solution on other forum :

ofstream outFile( variable_file_name.c_str());

the above could created the file with variable value as name ( in this
case, variable_file_name)
but i'm not quite clear of the concept

The constructor for std::eek:fstream accepts a const char* that is a pointer to
a c-style string of the filename to open. Such as:
std::eek:fstream Bar( "MyFile.txt" );
std::eek:fstream Bar( "file.ext" );
or whatever. In these cases the c-strings are constant.

Now, std::string has a method called c_str() that will point to the contents
of the string null terminated, which is what a c-style string is. A null
terminated array of characters. So, if we do:
std::string Foo = "MyFile.ext";
std::eek:fstream Bar( Foo.c_str() );
Foo.c_str() returns a char pointer that will point to "MyFile.ext".

Clear?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,275
Messages
2,571,375
Members
48,067
Latest member
MackenzieP

Latest Threads

Top