about the use of a string in an ifstream statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Herv? LEBAIL

    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++:26 1: error: no matching function for call to `
    std::basic_ifst ream<char, std::char_trait s<char> >::basic_ifstre am(
    std::string&, const std::_Ios_Openm ode&)'
    /usr/local/bin/gcc3_3/include/c++/3.3/iosfwd:89: error: candidates
    are:
    std::basic_ifst ream<char, std::char_trait s<char>[color=blue]
    >::basic_ifstre am(const[/color]
    std::basic_ifst ream<char, std::char_trait s<char> >&)
    /usr/local/bin/gcc3_3/include/c++/3.3/fstream:519: error:
    std::basic_ifst ream<_CharT, _Traits>::basic _ifstream(const char*,
    std::_Ios_Openm ode = std::ios_base:: in) [with _CharT = char,
    _Traits =
    std::char_trait s<char>]
    /usr/local/bin/gcc3_3/include/c++/3.3/fstream:504: error:
    std::basic_ifst ream<_CharT, _Traits>::basic _ifstream() [with _CharT
    = char,
    _Traits = std::char_trait s<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::i n), 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
  • Victor Bazarov

    #2
    Re: about the use of a string in an ifstream statement

    "Herv? LEBAIL" <lebail_herve@h otmail.com> wrote...[color=blue]
    > I'm writing a program which use the <string>, <vector> and <ifstream>
    > classes.[/color]

    Have you actually looked at what 'ifstream's constructor needs?
    [color=blue]
    >
    > 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++:26 1: error: no matching function for call to `
    > std::basic_ifst ream<char, std::char_trait s<char> >::basic_ifstre am(
    > std::string&, const std::_Ios_Openm ode&)'
    > /usr/local/bin/gcc3_3/include/c++/3.3/iosfwd:89: error: candidates
    > are:
    > std::basic_ifst ream<char, std::char_trait s<char>[color=green]
    > >::basic_ifstre am(const[/color]
    > std::basic_ifst ream<char, std::char_trait s<char> >&)
    > /usr/local/bin/gcc3_3/include/c++/3.3/fstream:519: error:
    > std::basic_ifst ream<_CharT, _Traits>::basic _ifstream(const char*,
    > std::_Ios_Openm ode = std::ios_base:: in) [with _CharT = char,
    > _Traits =
    > std::char_trait s<char>]
    > /usr/local/bin/gcc3_3/include/c++/3.3/fstream:504: error:
    > std::basic_ifst ream<_CharT, _Traits>::basic _ifstream() [with _CharT
    > = char,
    > _Traits = std::char_trait s<char>]
    >
    > It's (I guess) probably due to the use of a string or the absence of
    > quotes in :[/color]

    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'.
    [color=blue]
    >
    > ifstream file(file_names[in_file],ios::in);
    >
    > I've tested instead ifstream
    > file("\""+file_ names[in_file]+"\"",ios::i n), the quotes are then
    > present in the string but It still doesn't work.[/color]

    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());
    [color=blue]
    >
    > I've checked that ifstream file("file1.txt ",ios::in) is OK for (one
    > after the others) all the files.[/color]

    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.
    [color=blue]
    >
    > If soeone has got an idea ...[/color]

    See above.

    V


    Comment

    • Rob Williscroft

      #3
      Re: about the use of a string in an ifstream statement

      Herv? LEBAIL wrote in news:1373ddb7.0 402090727.329d4 c76
      @posting.google .com:
      [color=blue]
      >
      > 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);[/color]

      change to:

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

      [color=blue]
      > // 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) :
      >[/color]

      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.
      --

      Comment

      • Karl Heinz Buchegger

        #4
        Re: about the use of a string in an ifstream statement

        Herv? LEBAIL wrote:[color=blue]
        >
        > 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[/color]

        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
        kbuchegg@gascad .at

        Comment

        • Sumit Rajan

          #5
          Re: about the use of a string in an ifstream statement


          "Herv? LEBAIL" <lebail_herve@h otmail.com> wrote in message
          news:1373ddb7.0 402090727.329d4 c76@posting.goo gle.com...[color=blue]
          > 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);[/color]

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


          HTH,
          Sumit.

          [color=blue]
          > // 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++:26 1: error: no matching function for call to `
          > std::basic_ifst ream<char, std::char_trait s<char> >::basic_ifstre am(
          > std::string&, const std::_Ios_Openm ode&)'
          > /usr/local/bin/gcc3_3/include/c++/3.3/iosfwd:89: error: candidates
          > are:
          > std::basic_ifst ream<char, std::char_trait s<char>[color=green]
          > >::basic_ifstre am(const[/color]
          > std::basic_ifst ream<char, std::char_trait s<char> >&)
          > /usr/local/bin/gcc3_3/include/c++/3.3/fstream:519: error:
          > std::basic_ifst ream<_CharT, _Traits>::basic _ifstream(const char*,
          > std::_Ios_Openm ode = std::ios_base:: in) [with _CharT = char,
          > _Traits =
          > std::char_trait s<char>]
          > /usr/local/bin/gcc3_3/include/c++/3.3/fstream:504: error:
          > std::basic_ifst ream<_CharT, _Traits>::basic _ifstream() [with _CharT
          > = char,
          > _Traits = std::char_trait s<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::i n), 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[/color]


          Comment

          • David Harmon

            #6
            Re: about the use of a string in an ifstream statement

            On 9 Feb 2004 07:27:49 -0800 in comp.lang.c++, lebail_herve@ho tmail.com
            (Herv? LEBAIL) was alleged to have written:[color=blue]
            > ifstream file(file_names[in_file],ios::in);[/color]

            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::i n);

            Comment

            • DrUg13

              #7
              Re: about the use of a string in an ifstream statement

              ostringstream results;
              //Get file ready
              ifstream data;
              data.open(fileN ame.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, lebail_herve@ho tmail.com (Herv? LEBAIL)
              wrote:
              [color=blue]
              >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++:2 61: error: no matching function for call to `
              > std::basic_ifst ream<char, std::char_trait s<char> >::basic_ifstre am(
              > std::string&, const std::_Ios_Openm ode&)'
              >/usr/local/bin/gcc3_3/include/c++/3.3/iosfwd:89: error: candidates
              >are:
              > std::basic_ifst ream<char, std::char_trait s<char>[color=green]
              >>::basic_ifstr eam(const[/color]
              > std::basic_ifst ream<char, std::char_trait s<char> >&)
              >/usr/local/bin/gcc3_3/include/c++/3.3/fstream:519: error:
              > std::basic_ifst ream<_CharT, _Traits>::basic _ifstream(const char*,
              > std::_Ios_Openm ode = std::ios_base:: in) [with _CharT = char,
              >_Traits =
              > std::char_trait s<char>]
              >/usr/local/bin/gcc3_3/include/c++/3.3/fstream:504: error:
              > std::basic_ifst ream<_CharT, _Traits>::basic _ifstream() [with _CharT
              >= char,
              > _Traits = std::char_trait s<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::i n), 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[/color]

              Comment

              Working...