So I tried to do this:
#include <iostream>//For cout/cin
#include <fstream> //For ifstream/ofstream
using namespace std;
int main()
{
string types[] = {"Creativity", "Action", "Service"};
for(int i = 0; i < sizeof(types)/sizeof(string); i++) {
string type = types[i];
string filename = type + ".html";
ofstream newFile(filename);
//newFile << toHTML(getActivities(type));
newFile.close();
}
return 0;
}
and I’m being hit with errors. I’m new to C++, so I don’t know what to try, or if this is even possible (SURELY it is…).
I tried the following, but it was really just a stab in the dark and didn’t help:
#include <iostream>//For cout/cin
#include <fstream> //For ifstream/ofstream
using namespace std;
int main()
{
string types[] = {"Creativity", "Action", "Service"};
for(int i = 0; i < sizeof(types)/sizeof(string); i++) {
string type = types[i];
//Attempting to add const..
const string filename = type + ".html";
ofstream newFile(filename);
//newFile << toHTML(getActivities(type));
newFile.close();
}
return 0;
}
I mean, its all happy if I do `ofstream newFile(“somefile.html”);
The original IOstream library didn’t have a constructor taking a
std::string. The only type supported waschar const*. You can get achar const*from astd::stringusingc_str():The type of a string literal isn’t of type
std::stringbut it is of typechar const[n]wherenis the number of characters in the string, including the terminating null character.In C++ 2011 the File stream classes are improved to also take
std::stringwhere a string is expected.