string class

This is a discussion on string class within the C++ Programming forums, part of the General Programming Boards category; I am having a problem with ifstream and strings. I have: #include<iostream> #include<fstream> #include<string> #include<cstring.> #include<iomanip.h> other declarations .................................. ...................... ...

  1. #1
    jjg
    jjg is offline
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    Question string class

    I am having a problem with ifstream and strings.

    I have:

    #include<iostream>
    #include<fstream>
    #include<string>
    #include<cstring.>
    #include<iomanip.h>

    other declarations ..................................
    ......................

    string filename;
    ifstream fin;

    cout<<"enter filename";

    getline(cin, filename);

    fin.open(filename.data());*****when compiler gets to this point
    **it says data is not a member of string

    I checked in my book and it says that s.data() is part of the string class. I was hoping that someone could help me to figure out why I can't compile past this point. I am using Borland C++ 5.02.

    thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I thought it was...

    string filename;

    filename.str( );
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    I don't think this should work anyway, data() returns an array with each character in the string an element in the array but the last element isn't a null character. c_str() returns the same as data() but there's a null character at the end, open needs that null to know where to stop. So your call should be more like this.
    Code:
    fin.open( filename.c_str() );
    You don't need the period after cstring and iomanip doesn't have an extension. Make sure that somewhere you have using namespace std; declared or none of this stuff will work.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21