Question

I've given an assignment where I need to take some input from a file named like: "filename.s" and my code should write the output to a file named "filename.m". here's my code but i'm having compiling issues when i try to open the file as outfile.open(out);

int main(int argc, char *argv[]){
  readFile(argv);
  int x=0;
  compile(x);
  mystring += "halt";
  cout << mystring<< endl;
  string out = argv[1];
  out.resize(out.size()-1);
  out += "m";
  ofstream outfile;
  outfile.open(out);
  outfile << mystring;
  outfile.close();
  return 0;
}

Anyone knows what might be the issue? Because it does compile when i give an argument like this: outfile.open("blah.m");thanks for your replies.

Was it helpful?

Solution

I guess you use an older compiler without C++11, so the line

outfile.open(out);

fails to compile since ni C++98 open accepts character pointers only no std::strings. Change the line to

outfile.open(out.c_str());

and it should compile

OTHER TIPS

Try this:

outfile.open(out.c_str());

In C++98, the parameter of open() is const char *.

Easier solution, but rather specific to this case:

size_t pos = strlen(argv[1]) - 1;
argv[1][pos] = 'm';

std::ofstream out(argv[1]); // Easier to just create the stream already opened.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top