C++ error

I'm trying to make a program in which you run the program in command prompt, and you type a message that is saved in a file. That file's dir is typed in the arguments when you first run it from command prompt. For some reason I always get and error and I'm not sure why I get an error. Can someone explain to me why I'm getting an error and what I need to fix?

Code:
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]){
string dir(argv);
string wrote;
ofstream file;
file.open(dir);

cout << "Enter what is needed to be typed in " << dir << " then click ctrl-z: " << endl;
cin >> wrote;
file >> wrote;

file.close();

return 0;
}
Last edited on
argv is a char**, so string dir(argv); makes no sense. It should be string dir(*argv); or string dir(argv[0]);

The first "string" in argv is the name of the program.
The first parameter is in argv[1].
Before reading it you should make sure an argument was provided:
1
2
3
4
5
6
7
8
string dir;
if( 2 == argc )
    dir = argv[1];
else
{
    cout << "Wrong number of arguments" << endl;
    return 1;
}


Also, file >> wrote; reads from the file.
To write to it, it's file << wrote;
Topic archived. No new replies allowed.