Reputation: 107
hi i am having a bit of trouble with this code
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line;
//const char* test = "connect";
#define test "connect"
string con("c:\\filepath\\%s.txt", test);
ifstream file;
file.open(con.c_str());
if (file.is_open()) {
while (file.good()) {
getline(file, line);
printf("setc %s\n", line.c_str());
//cout << "setc " << line << endl;
}
file.close();
} else
cout << "Unable to open file";
return 0;
}
could someone please tell me what i am going wrong
this is what i am after
'con' is meant to get its filename from 'test'
if you could help me i would appreciate it :)
Upvotes: 1
Views: 790
Reputation: 153820
There are many ways to do this. Here are two:
std::string con1("c:\\filepath\\" test ".txt");
std::string con2("c:\\filepath\\" + std::string(test) + ".txt");
The initialization of con1
requires that test
becomes a string literal through macro expansion as it relies on the merging of of string literals. The second form is more general and test
can be anything which can be converted to a std::string
, e.g., a char const*
.
Upvotes: 3