Can you help me? I am truna to create a program with supporting cmd
->int main(int argc, char *argv[])
. Using class, I want to create a method for writting my infromation. The name of the .txt file is third argument.
My problem is that I need to open()
my file, but the name of the file can be changed. I tried to open the specified file it's 1.txt
. But How should I do so in order to give the name of the file in cmd
because my method, which is in Header
, see that name?
My method in header:
void recordData()
{
wRecord.open("1.txt", std::ios_base::app);//the hard way to create a file in this method.
int i=0;
wRecord << "Name of your city: " << Name << std::endl;
......................................................
wRecord << "Quanity of schools in your city: " << Schools << std::endl;
wRecord << std::endl;
}
But it should be wRecord.open(argv[3], std::ios_base::app);
,I think. But how correctly give argv[3]
?
My code ,when I call void recordData()
:
if (argv[1] == create)
{
wRecord.open(argv[3], std::ios_base::app);// here is I put any name of file
//But I can't use any name because in method there is only `1.txt`
if (!wRecord)
{
std::cerr << "Error: canNOT oprn a text file" << std::endl;
exit(-1);
}
}