about the use of a string in an ifstream statement

H

Herv? LEBAIL

Hi everybody,

I'm writing a program which use the <string>, <vector> and <ifstream>
classes.

Given an array of string, i.e vector<string> file_names, example :

file_names[0] = "file1.txt"
file_names[1] = "file2.txt"
etc ...

I want to do a for-loop and read the content of all of this files, I
started with (n_file is the size of the dynamical allocated array
file_names) :

for (in_file = 0; in_file < n_file; ++in_file)
{
ifstream file(file_names[in_file],ios::in);
// here the data handling of the content of the file
file.close();
}

It doesn't work, I get a message error during the compiling process
(gnu gcc comiler on linux) :

opt_main.c++:261: error: no matching function for call to `
std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(
std::string&, const std::_Ios_Openmode&)'
/usr/local/bin/gcc3_3/include/c++/3.3/iosfwd:89: error: candidates
are:
std::basic_ifstream said:
::basic_ifstream(const
std::basic_ifstream<char, std::char_traits<char> >&)
/usr/local/bin/gcc3_3/include/c++/3.3/fstream:519: error:
std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*,
std::_Ios_Openmode = std::ios_base::in) [with _CharT = char,
_Traits =
std::char_traits<char>]
/usr/local/bin/gcc3_3/include/c++/3.3/fstream:504: error:
std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT
= char,
_Traits = std::char_traits<char>]

It's (I guess) probably due to the use of a string or the absence of
quotes in :

ifstream file(file_names[in_file],ios::in);

I've tested instead ifstream
file("\""+file_names[in_file]+"\"",ios::in), the quotes are then
present in the string but It still doesn't work.

I've checked that ifstream file("file1.txt",ios::in) is OK for (one
after the others) all the files.

If soeone has got an idea ...

Thanks in advance

Herve
 
V

Victor Bazarov

Herv? LEBAIL said:
I'm writing a program which use the <string>, <vector> and <ifstream>
classes.

Have you actually looked at what 'ifstream's constructor needs?
Given an array of string, i.e vector<string> file_names, example :

file_names[0] = "file1.txt"
file_names[1] = "file2.txt"
etc ...

I want to do a for-loop and read the content of all of this files, I
started with (n_file is the size of the dynamical allocated array
file_names) :

for (in_file = 0; in_file < n_file; ++in_file)
{
ifstream file(file_names[in_file],ios::in);
// here the data handling of the content of the file
file.close();
}

It doesn't work, I get a message error during the compiling process
(gnu gcc comiler on linux) :

opt_main.c++:261: error: no matching function for call to `
std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(
std::string&, const std::_Ios_Openmode&)'
/usr/local/bin/gcc3_3/include/c++/3.3/iosfwd:89: error: candidates
are:
std::basic_ifstream said:
::basic_ifstream(const
std::basic_ifstream<char, std::char_traits<char> >&)
/usr/local/bin/gcc3_3/include/c++/3.3/fstream:519: error:
std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*,
std::_Ios_Openmode = std::ios_base::in) [with _CharT = char,
_Traits =
std::char_traits<char>]
/usr/local/bin/gcc3_3/include/c++/3.3/fstream:504: error:
std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT
= char,
_Traits = std::char_traits<char>]

It's (I guess) probably due to the use of a string or the absence of
quotes in :

No, it's not due to absence of quotes. It's due to the fact that
ifstream needs 'const char*' as the first argument of the constructor
and you're shoving it 'string'.
ifstream file(file_names[in_file],ios::in);

I've tested instead ifstream
file("\""+file_names[in_file]+"\"",ios::in), the quotes are then
present in the string but It still doesn't work.

Of course it doesn't work. You need to get 'const char*' out of your
'string' object. To do that use 'c_str()' function:

ifstream file(file_names[in_file].c_str());
I've checked that ifstream file("file1.txt",ios::in) is OK for (one
after the others) all the files.

Of course it's OK. The string literal (not to be confused with
an object of type 'std::string') has the type "const char[]", which
is convertible to "const char*", while an 'std::string' isn't.
If soeone has got an idea ...

See above.

V
 
S

Sumit Rajan

Herv? LEBAIL said:
Hi everybody,

I'm writing a program which use the <string>, <vector> and <ifstream>
classes.

Given an array of string, i.e vector<string> file_names, example :

file_names[0] = "file1.txt"
file_names[1] = "file2.txt"
etc ...

I want to do a for-loop and read the content of all of this files, I
started with (n_file is the size of the dynamical allocated array
file_names) :

for (in_file = 0; in_file < n_file; ++in_file)
{
ifstream file(file_names[in_file],ios::in);

ifstream file(file_names[in_file].c_str());


HTH,
Sumit.

// here the data handling of the content of the file
file.close();
}

It doesn't work, I get a message error during the compiling process
(gnu gcc comiler on linux) :

opt_main.c++:261: error: no matching function for call to `
std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(
std::string&, const std::_Ios_Openmode&)'
/usr/local/bin/gcc3_3/include/c++/3.3/iosfwd:89: error: candidates
are:
std::basic_ifstream said:
::basic_ifstream(const
std::basic_ifstream<char, std::char_traits<char> >&)
/usr/local/bin/gcc3_3/include/c++/3.3/fstream:519: error:
std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*,
std::_Ios_Openmode = std::ios_base::in) [with _CharT = char,
_Traits =
std::char_traits<char>]
/usr/local/bin/gcc3_3/include/c++/3.3/fstream:504: error:
std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT
= char,
_Traits = std::char_traits<char>]

It's (I guess) probably due to the use of a string or the absence of
quotes in :

ifstream file(file_names[in_file],ios::in);

I've tested instead ifstream
file("\""+file_names[in_file]+"\"",ios::in), the quotes are then
present in the string but It still doesn't work.

I've checked that ifstream file("file1.txt",ios::in) is OK for (one
after the others) all the files.

If soeone has got an idea ...

Thanks in advance

Herve
 
R

Rob Williscroft

Herv? LEBAIL wrote in @posting.google.com:
I'm writing a program which use the <string>, <vector> and <ifstream>
classes.

Given an array of string, i.e vector<string> file_names, example :

file_names[0] = "file1.txt"
file_names[1] = "file2.txt"
etc ...

I want to do a for-loop and read the content of all of this files, I
started with (n_file is the size of the dynamical allocated array
file_names) :

for (in_file = 0; in_file < n_file; ++in_file)
{
ifstream file(file_names[in_file],ios::in);

change to:

ifstream file( file_names[in_file].c_str() ,ios::in );

// here the data handling of the content of the file
file.close();
}

It doesn't work, I get a message error during the compiling process
(gnu gcc comiler on linux) :

For some reason the ?fstream ctors only take filename arguments
by char const *, not std::string const &, use std::string's c_str()
member function to get the char const * needed.

HTH.

Rob.
 
K

Karl Heinz Buchegger

Herv? LEBAIL said:
Hi everybody,

I'm writing a program which use the <string>, <vector> and <ifstream>
classes.

Given an array of string, i.e vector<string> file_names, example :

file_names[0] = "file1.txt"
file_names[1] = "file2.txt"
etc ...

I want to do a for-loop and read the content of all of this files, I
started with (n_file is the size of the dynamical allocated array
file_names) :

for (in_file = 0; in_file < n_file; ++in_file)
{
ifstream file(file_names[in_file],ios::in);
// here the data handling of the content of the file

Well. Look up your documentation of what the first argument to the
constructor can be.
It can be a 'const char *', but not a std::string.
But you can ask a std::string for a const char* by using
it's member function c_str()

ifstream file( file_names[in_file].c_str(), ios::in );
 
D

David Harmon

ifstream file(file_names[in_file],ios::in);

ifstream has no constructor that knows std::string. You have to use the
string::c_str() function to convert

ifstream file(file_names[in_file].c_str(),ios::in);
 
D

DrUg13

ostringstream results;
//Get file ready
ifstream data;
data.open(fileName.c_str());

results << data.rdbuf(); // put entire file into results
data.close(); // close file were done with it
string temp = results.str();

Now the entire file is now in a string object


Hi everybody,

I'm writing a program which use the <string>, <vector> and <ifstream>
classes.

Given an array of string, i.e vector<string> file_names, example :

file_names[0] = "file1.txt"
file_names[1] = "file2.txt"
etc ...

I want to do a for-loop and read the content of all of this files, I
started with (n_file is the size of the dynamical allocated array
file_names) :

for (in_file = 0; in_file < n_file; ++in_file)
{
ifstream file(file_names[in_file],ios::in);
// here the data handling of the content of the file
file.close();
}

It doesn't work, I get a message error during the compiling process
(gnu gcc comiler on linux) :

opt_main.c++:261: error: no matching function for call to `
std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(
std::string&, const std::_Ios_Openmode&)'
/usr/local/bin/gcc3_3/include/c++/3.3/iosfwd:89: error: candidates
are:
std::basic_ifstream said:
::basic_ifstream(const
std::basic_ifstream<char, std::char_traits<char> >&)
/usr/local/bin/gcc3_3/include/c++/3.3/fstream:519: error:
std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*,
std::_Ios_Openmode = std::ios_base::in) [with _CharT = char,
_Traits =
std::char_traits<char>]
/usr/local/bin/gcc3_3/include/c++/3.3/fstream:504: error:
std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT
= char,
_Traits = std::char_traits<char>]

It's (I guess) probably due to the use of a string or the absence of
quotes in :

ifstream file(file_names[in_file],ios::in);

I've tested instead ifstream
file("\""+file_names[in_file]+"\"",ios::in), the quotes are then
present in the string but It still doesn't work.

I've checked that ifstream file("file1.txt",ios::in) is OK for (one
after the others) all the files.

If soeone has got an idea ...

Thanks in advance

Herve
 

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
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top