Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using a string to indicate a filename

Status
Not open for further replies.

vdoerga

Programmer
Aug 4, 2004
1
NL
Hello people,

I'm having a problem I hope you can help me with. I'm writing a program which does the following:
1. reads a .txt file that contains filenames and puts the filenames into a vector as strings
2. walks through the vector entries and for each file, opens the file, reads each line, adds the filename at the end of each line and writes the lines to a new file (which is nothing more thatn the same old file, with the filename added at the end of each line).

here is a beginning to my source code:

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

using namespace std;

int main ()
{
vector<string> bnamen;
string regel;
ifstream lijst("dirlst.txt", ios::in);

while (getline(lijst, regel)) {
bnamen.push_back(regel);
}
for (int i=0;i<bnamen.size();i++){
cout<<bnamen<<endl;
}
string regel2;
** ifstream invoer((bnamen[0]), ios::in);
while(getline(invoer, regel2)){
cout<<regel2<<endl;
}
invoer.close();
lijst.close();
cin.get();
return 0;
}

The line marked with ** is where there is an error. Apparently I can't use a string as a filename to be opened with ifstream. As soon as put the double qoutes (like "bnamen[0]") it doesn't show up as an error, but of course that is not the correct filename. I use Microsoft Developer Studio, it doesn't give explanation when there are errors, so I have no detailed error description.

I hope that someone can help me!
 
Hi,

The constructor of the class ifstream requires a variable of type "const char *" instead of "const string&" as its first argument.
So you have to translate your string objects with string::c_str (), like that :
Code:
 ifstream invoer(bnamen[0].c_str (), ios::in);

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top