file input problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pentiumPunk

    file input problem

    string fileName;

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

    ifstream fin;

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


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

    Thanks alot in ahead!


  • John Harrison

    #2
    Re: file input problem


    "pentiumPun k" <Broox@triad.rr .com> wrote in message
    news:wpe5b.4437 7$5H4.2243019@t wister.southeas t.rr.com...[color=blue]
    > string fileName;
    >
    > cout<<"Enter filename of datafile you wish to open \n";
    > cin>>fileName;
    >
    > ifstream fin;
    >
    > fin.open(fileNa me, ifstream::ios);
    > if(!fin)
    > {
    > cerr<<"blah blah blah";
    > exit(1);
    > }
    >
    >
    > i get this error message in VC++ .....Error C2664...on the
    > fin.open(fileNa me, ifstream::ios); line...cannot convert parameter 1 from
    > std::string to const char *
    > i cannot put a certain filename down such as fin.open("textf ile.txt"), i
    > need the user to enter its name, for it will change.
    >
    > Thanks alot in ahead!
    >[/color]

    fin.open(fileNa me.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


    Comment

    • Sandeep

      #3
      Re: file input problem

      "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:<bj3smh$ep 038$1@ID-196037.news.uni-berlin.de>...[color=blue]
      > "pentiumPun k" <Broox@triad.rr .com> wrote in message
      > news:wpe5b.4437 7$5H4.2243019@t wister.southeas t.rr.com...[color=green]
      > > string fileName;
      > >
      > > cout<<"Enter filename of datafile you wish to open \n";
      > > cin>>fileName;
      > >
      > > ifstream fin;
      > >
      > > fin.open(fileNa me, ifstream::ios);
      > > if(!fin)
      > > {
      > > cerr<<"blah blah blah";
      > > exit(1);
      > > }
      > >
      > >
      > > i get this error message in VC++ .....Error C2664...on the
      > > fin.open(fileNa me, ifstream::ios); line...cannot convert parameter 1 from
      > > std::string to const char *
      > > i cannot put a certain filename down such as fin.open("textf ile.txt"), i
      > > need the user to enter its name, for it will change.
      > >
      > > Thanks alot in ahead!
      > >[/color][/color]
      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(filena me.c_str(),ifst ream::ios) instead of fin.open(fileNa me,
      ifstream::ios)

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

      Comment

      • Thomas Matthews

        #4
        Re: file input problem

        pentiumPunk wrote:
        [color=blue]
        > string fileName;
        >
        > cout<<"Enter filename of datafile you wish to open \n";
        > cin>>fileName;
        >
        > ifstream fin;
        >
        > fin.open(fileNa me, ifstream::ios);
        > if(!fin)
        > {
        > cerr<<"blah blah blah";
        > exit(1);
        > }
        >
        >
        > i get this error message in VC++ .....Error C2664...on the
        > fin.open(fileNa me, ifstream::ios); line...cannot convert parameter 1 from
        > std::string to const char *
        > i cannot put a certain filename down such as fin.open("textf ile.txt"), i
        > need the user to enter its name, for it will change.
        >
        > Thanks alot in ahead![/color]

        1. As others have stated:
        fin.open(filena me.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::out" ,
        or "ios_base::bina ry". 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:

        C++ Faq: http://www.parashift.com/c++-faq-lite
        C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        alt.comp.lang.l earn.c-c++ faq:

        Other sites:
        http://www.josuttis.com -- C++ STL Library book

        Comment

        • Rolf Magnus

          #5
          Re: file input problem

          Sandeep wrote:
          [color=blue]
          > "John Harrison" <john_andronicu s@hotmail.com> wrote in message
          > news:<bj3smh$ep 038$1@ID-196037.news.uni-berlin.de>...[color=green]
          >> "pentiumPun k" <Broox@triad.rr .com> wrote in message
          >> news:wpe5b.4437 7$5H4.2243019@t wister.southeas t.rr.com...[color=darkred]
          >> > string fileName;
          >> >
          >> > cout<<"Enter filename of datafile you wish to open \n";
          >> > cin>>fileName;
          >> >
          >> > ifstream fin;
          >> >
          >> > fin.open(fileNa me, ifstream::ios);
          >> > if(!fin)
          >> > {
          >> > cerr<<"blah blah blah";
          >> > exit(1);
          >> > }
          >> >
          >> >
          >> > i get this error message in VC++ .....Error C2664...on the
          >> > fin.open(fileNa me, ifstream::ios); line...cannot convert parameter
          >> > 1 from std::string to const char *
          >> > i cannot put a certain filename down such as
          >> > fin.open("textf ile.txt"), i need the user to enter its name, for it
          >> > will change.
          >> >
          >> > Thanks alot in ahead!
          >> >[/color][/color]
          > When we declare any string variable it is not null terminated ie not
          > appended with '\0' .[/color]

          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.


          Comment

          Working...