Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: ifstream open problem with passing a string for text file name [duplicate]

i'm trying to pass a string from main to another function. this string is a name of text file that needs to be oepened. as far as i can see, i am passing the string alright, but when i try to use ifstream.open(textFileName), it doesn't quite work. but when i manually hardcode it as ifstream.open("foo.txt"), it works just fine. i would need to use this function several times so i would like to be able to pass in a string of text file name..

here's my main

#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif

#ifndef DATAREADER_H
#define DATAREADER_H
#include "DataReader.h"
#endif

using namespace std;

int main()
{
 vector<Data*> database = DataReader("foo.txt");

 return 0; 
}

header of DataReader

#include <fstream>
#include <iostream>
#include <vector>
#include <string>

#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif

using namespace std;

vector<Data*> DataReader(string textFile);

and finally the DataReader.cpp

#include "DataReader.h"

using namespace std;

vector<Data*> DataReader(string textFile)
{
 ifstream aStream;     
 aStream.open(textFile); //line 11

i looked up the ifstream.open() and it takes a string and a mode as parameters. not really sure what to do with the modes, but i tried them but they gave the same error message

DataReader.cpp: In function 'std::vector<Data*, std::allocator<Data*> > DataReader(std::string)':
DataReader.cpp:11: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
/usr/local/lib/gcc/sparc-sun-solaris2.9/4.0.3/../../../../include/c++/4.0.3/fstream:495: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]

thank you in advance for any input/suggestions.

Dean

like image 705
user200632 Avatar asked Nov 02 '09 17:11

user200632


People also ask

How to read a file and store in string in C++?

Read whole ASCII file into C++ std::string txt using file object f of ifstream type to perform read operation. Declare a variable str of string type. If(f) Declare another variable ss of ostringstream type. Call rdbuf() fuction to read data of file object.

How to declare string in file C++?

The C++ string class is defined in a library file named "string." If you want to use variables or parameters of type string in a program, you should add the line "#include <string>" at the beginning of the program.

Is ifstream a data type?

The ifstream Class. An ifstream is an input file stream, i.e. a stream of data used for reading input from a file. Because an ifstream IS an istream, anything you can do to an istream you can also do the same way to an ifstream.


2 Answers

the standard streams doesn't accept a standard string, only c-string! So pass the string using c_str():

aStream.open(textFile.c_str());
like image 90
Khaled Alshaya Avatar answered Sep 21 '22 00:09

Khaled Alshaya


Try this:

aStream.open(textFile.c_str()); //line 11

I think your code needs to take the internal C string to pass to the open() call. Note I'm not at a compiler right now, so can't double check this.

You may also want to check the signature of the this method:

vector<Data*> DataReader(string textFile);

Here, a complete copy of the vector will be taken when it's returned from the method, which could be computationally expensive. Note, it won't copy the Data objects, just the pointers, but with a lot of data might not be a good idea. Similarly with the string input.

Consider this instead:

void DataReader( const string& textFile, vector<Data*>& dataOut );
like image 41
pxb Avatar answered Sep 23 '22 00:09

pxb