I am trying to write a file that writes to a user specified folder using ofstream. The basics of the code looks like this:

#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

int write(int id)
{
  ofstream myfile;
  myfile.open ("account/"id"/example.txt"); //this is where the error pops up
  myfile << "this is what I want written";
  myfile.close();   
}

int Account()
{
  int id;
  cout << "Please enter account" << endl;
  cin >> id;
  return id;   
}

int main() 
{
  int id;
  id = Account();
  write(id);
  return 0;
}

Anyways, hopefully this is an easy question as I havn't programmed since highschool. Thanks in advance.

>>hopefully this is an easy question
Ok, I'm not a mind reader so please state the question.

delete stdlib.h and conio.h because they are not needed in c++.

question was more of a statement but still there

at the point where I'm trying to access the file

myfile.open ("account/"id"/example.txt"); //this is where the error pops up

I want to have the program write to a file based on the account name (id). The actual questions are what is the proper way to do that and is there a better way?

stdlib is there out of habit, I use conio.h for system("CLS")

int write(int id)
{
  ofstream myfile;
  myfile.open ("account/"id"/example.txt"); //this is where the error pops up
  myfile << "this is what I want written";
  myfile.close();   
}

Yep, there's definitely an error there. The double quote at the beginning of id closes off the string for the open command, then you have stuff the compiler does not know what to do with.

You need to convert the integer parameter to its string representation, then build the string you want to use for the open command's argument.

You might simplify it all if you'd originally taken the account ID as a string instead of an int, and it would make your input more bulletproof.

The error is because you have embedded the quote sysmble " within the path -- you can't do that. You will have to format the filename before calling the open function, something like this:

#include <sstream>
#include <string>
#include <fstream>
using namespace std; 

int main()
{
    ofstream myfile;
     int id = 12345;  // some id
     stringstream str;
     str << "account/" << id << "/example.txt";
     myfile.open(str.str());

     return 0;
}

awesome that makes sense but now I've run into a new problem.

myfile.open (id);

the compiler is saying no matching function or call to std::basic and then goes on to define the write part. I'm assuming that I need to tell it id is the path I want opened. Is this true or how else can I fix this.

This is the whole function as it stands at the moment, I basically changed everything to the strings and copied and pasted your string into the main.

int write(string id)
{
  ofstream myfile;
  myfile.open (id);
  myfile << "this is what I want written";
  myfile.close();   
}
This article has been dead for over six months. Start a new discussion instead.