file input problem

P

pentiumPunk

string fileName;

cout<<"Enter filename of datafile you wish to open \n";
cin>>fileName;

ifstream fin;

fin.open(fileName, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}


i get this error message in VC++ .....Error C2664...on the
fin.open(fileName, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textfile.txt"), i
need the user to enter its name, for it will change.

Thanks alot in ahead!
 
J

John Harrison

pentiumPunk said:
string fileName;

cout<<"Enter filename of datafile you wish to open \n";
cin>>fileName;

ifstream fin;

fin.open(fileName, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}


i get this error message in VC++ .....Error C2664...on the
fin.open(fileName, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textfile.txt"), i
need the user to enter its name, for it will change.

Thanks alot in ahead!

fin.open(fileName.c_str(), ifstream::ios);

As the error message says, open requires a const char*, c_str returns a
const char* from a string.

Its just one of the quirks of the standard library.

john
 
S

Sandeep

When we declare any string variable it is not null terminated ie not
appended with '\0' .Since in the above program u r using fstream which
requires filename to be null terminated so we can make it null
terminated by using c_str() which convert it to null terminated string
..ie use
fin.open(filename.c_str(),ifstream::ios) instead of fin.open(fileName,
ifstream::ios)

Enjoy c++ programming........
 
T

Thomas Matthews

pentiumPunk said:
string fileName;

cout<<"Enter filename of datafile you wish to open \n";
cin>>fileName;

ifstream fin;

fin.open(fileName, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}


i get this error message in VC++ .....Error C2664...on the
fin.open(fileName, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textfile.txt"), i
need the user to enter its name, for it will change.

Thanks alot in ahead!

1. As others have stated:
fin.open(filename.c_str());
or
ifstream fin(filename.c_str());

2. What kind of file mode is "ifstream::ios"?
Try a combination of: "ios_base::in", "ios_base::eek:ut",
or "ios_base::binary". The default mode for ifstream
is "ios_base::in" as a text file.

3. Don't use the exit() function. It has some nasty quirks to it.
Use the return statement instead.

4. Instead of returning a '1' from main() to the operating system,
use the predefined, portable constants EXIT_SUCCESS or
EXIT_FAILURE as defined in <cstdlib>.

5. You may want to change:
cin >> fileName;
to
getline(cin, filename, '\n');
The getline() function will read in the whole line. The other
expression _may_ not read in spaces or other characters.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Rolf Magnus

Sandeep said:
When we declare any string variable it is not null terminated ie not
appended with '\0' .

What do you mean by "any string variable"? If you're talking about
std::string, then it's not defined (and shouldn't matter) how the data
is stored internally. The only thing that matters is that c_str()
returns a null terminated array of the string's content.
An implementation of std::string that works on null terminated
characters internally (and thus doesn't need any extra work for
c_str() would be valid, too.
 

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

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top