473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

about the use of a string in an ifstream statement

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<char, std::char_traits<char>
::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
Jul 22 '05 #1
6 3593
"Herv? LEBAIL" <le**********@hotmail.com> wrote...
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<char, std::char_traits<char>
::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
Jul 22 '05 #2
Herv? LEBAIL wrote in news:1373ddb7.0402090727.329d4c76
@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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Herv? LEBAIL wrote:

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 );
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

"Herv? LEBAIL" <le**********@hotmail.com> wrote in message
news:13**************************@posting.google.c om...
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<char, std::char_traits<char>
::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

Jul 22 '05 #5
On 9 Feb 2004 07:27:49 -0800 in comp.lang.c++, le**********@hotmail.com
(Herv? LEBAIL) was alleged to have written:
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);

Jul 22 '05 #6
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
On 9 Feb 2004 07:27:49 -0800, le**********@hotmail.com (Herv? LEBAIL)
wrote:
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<char, std::char_traits<char>
::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


Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
11
by: kazack | last post by:
I am under the the impression that a variable is what is stored in a memory address and a pointer is the memory address of the variable? I was looking to use a function that does not return a...
4
by: Daniel Fudge | last post by:
I can't send a string from a dymanic array to a function. At the getpts function declarationon (line 14), I get the following error. "main.cpp(14) : error C2664: 'void __thiscall...
4
by: marathoner | last post by:
I tried your advice, and replaced "ifstream" with "std::ifstream". I also replaced instances of "ofstream" with "std::ofstream". Those syntax errors were resolved. When I added "std" to the...
5
by: Assertor | last post by:
Hi, all. Is there any way to create an instance of std::ifstream using std::string. (through std::ifstream's constructor or assignment operator or iterator, etc...) i.e. std::string str =...
7
by: random guy | last post by:
Hi, I'm writing a program which creates an index of text files. For each file it processes, the program records the start and end positions (as returned by tellg()) of sections of interest,...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
4
by: Chris Forone | last post by:
hello group, why have i to bracket the second ctor param in the following example? thx & hand, chris #include <fstream> #include <iterator> int main()
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.