-
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
-
I thought it was...
string filename;
filename.str( );
-
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.