469,602 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

const string & to fopen..

Jae
Real(const string &fileName)
{

FILE * myInputFile = fopen(fileName, "rt");

.....

fclose(myInputFile);
}
I'd like to read a file names from fileName. However, I always got an error

error: cannot convert `const std::basic_string<char, std::char_traits<char>,
std::allocator<char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?

I tried

fopen((const char) fileName, "rt");

but it shows

error: `const struct std::basic_string<char, std::char_traits<char>,
std::allocator<char> >' used where a `const char' was expected
Can anyone help me?
May 2 '06 #1
5 8332
Jae wrote:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
fclose(myInputFile);
}
I'd like to read a file names from fileName. However, I always got an error

error: cannot convert `const std::basic_string<char, std::char_traits<char>,
std::allocator<char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?
fopen() expects a const char*, not a std::string (if that's what
"string" is). Just do

fopen(fileName.c_str(), "rt");
I tried

fopen((const char) fileName, "rt");


No. First, fopen() expects a const char* (a string), not a const char
(a character). Second, you cannot convert a std::string to a const
char.
Jonathan

May 2 '06 #2
Jae
Thank you and I can get as a line.
Well, but in this case, it reads only one line of the file. How can I read
whole file?
I'd like to read it as a string..
FILE* ifs = fopen(fileName.c_str(), "rt" );
if(!ifs)
{
cout<<"Cannot open a file"<<endl;
return;
}

fgets( totalString, 1000, ifs );

string myString;
for(int i = 0; i < 1000; i++){
if(!totalString[i]) break;
myString += totalString[i];
cout<<totalString[i];
}

It prints only one line, even though the file input has more than 10 lines.
Is my code wrong?


"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
Jae wrote:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
fclose(myInputFile);
}
I'd like to read a file names from fileName. However, I always got an
error

error: cannot convert `const std::basic_string<char,
std::char_traits<char>,
std::allocator<char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?


fopen() expects a const char*, not a std::string (if that's what
"string" is). Just do

fopen(fileName.c_str(), "rt");
I tried

fopen((const char) fileName, "rt");


No. First, fopen() expects a const char* (a string), not a const char
(a character). Second, you cannot convert a std::string to a const
char.
Jonathan

May 2 '06 #3
Jae wrote:
Thank you and I can get as a line.
Well, but in this case, it reads only one line of the file. How can I read
whole file?
I'd like to read it as a string..
FILE* ifs = fopen(fileName.c_str(), "rt" );
if(!ifs)
{
cout<<"Cannot open a file"<<endl;
return;
}

fgets( totalString, 1000, ifs );

string myString;
for(int i = 0; i < 1000; i++){
if(!totalString[i]) break;
myString += totalString[i];
cout<<totalString[i];
}

It prints only one line, even though the file input has more than 10 lines.
Is my code wrong?


Yes, fgets() stops reading after a newline. This may be of help:

# include <iostream>
# include <sstream>
# include <string>
# include <fstream>

int main()
{
std::ifstream ifs("file");

std::ostringstream oss;
oss << ifs.rdbuf();
std::string s(oss.str());

std::cout << s; // outputs whole file
}
Jonathan

May 2 '06 #4
Jae
I got it. Thanks a lot !!
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
Jae wrote:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
fclose(myInputFile);
}
I'd like to read a file names from fileName. However, I always got an
error

error: cannot convert `const std::basic_string<char,
std::char_traits<char>,
std::allocator<char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?


fopen() expects a const char*, not a std::string (if that's what
"string" is). Just do

fopen(fileName.c_str(), "rt");
I tried

fopen((const char) fileName, "rt");


No. First, fopen() expects a const char* (a string), not a const char
(a character). Second, you cannot convert a std::string to a const
char.
Jonathan

May 2 '06 #5
Jae wrote:
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
Jae wrote:
Real(const string &fileName)
{
FILE * myInputFile = fopen(fileName, "rt");
fclose(myInputFile);
}
I'd like to read a file names from fileName. However, I always got an
error

error: cannot convert `const std::basic_string<char,
std::char_traits<char>,
std::allocator<char> >' to `const char*' for argument `1' to `FILE*
fopen(const char*, const char*)'

How can I fix it ?
fopen() expects a const char*, not a std::string (if that's what
"string" is). Just do

fopen(fileName.c_str(), "rt");

I got it. Thanks a lot !!


Glad you did, but please do not top-post next time
(http://en.wikipedia.org/wiki/Top-post).
Jonathan

May 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by selder21 | last post: by
16 posts views Thread by Steven T. Hatton | last post: by
1 post views Thread by tggfang | last post: by
1 post views Thread by Erik Tamminga | last post: by
10 posts views Thread by lchian | last post: by
3 posts views Thread by sernamar | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.