passing filename as command line argument

Jul 29, 2010 at 6:30am
Hi there
I am working on a code where from command line user gives the name of the file in which output should be written. The file name is in array av[4]. I am unable to figure out how to make a file by the name stored in av[4] and write simple integer data in to the file.

I would really appreciate any help.


Thanks
Jul 29, 2010 at 7:14am
The question is not clear.
If u are getting the filename as a command line argument then you can use make fstream object as below:

ifstream in(argv[1]);

Or if u meant that file name is stored in the char array av[4];
then u can use following:

ifstream in(av);
Jul 29, 2010 at 7:27am
Hi Phoenix
Thanks for the reply. Let me make the question clear, this is the main function:
main(int ac, char** av)

in av all the command line arguments are there and av[4] stores the file name passed by the user, I want to make a file by the name stored in av array and write some data in it.

Example:
command line argument is
1 2 3 4 check.txt

I want to make a file check.txt and write some data in it.

I appreciate your help.

Thanks
Jul 29, 2010 at 7:31am
You create an output file stream like this (because av[0] is the program name):
ofstream out(av[5]);

and write some data in it.
Last edited on Jul 29, 2010 at 7:32am
Jul 29, 2010 at 7:40am
Athar I tried what you are saying but output generated was incorrect, then I copied av[5] to a char array and wrote this char array in a file and I found that some weird character is being written in to the file rather than the file name.
Jul 29, 2010 at 7:42am
My crystal ball is broken, so you'll need to show your code.
Jul 29, 2010 at 7:47am
ok heres the code:

void main(int ac, char** av) {
//
NumCamera=1;
NumTarget=2;
char temp[10];

hThread = CreateThread(NULL,0,ServerThread, 0,0, 0);


if ( ac == 5 )
{
NumCamera = atoi( av[1] );
NumTarget = atoi( av[2] );
port_for_tcp = atoi( av[3] );
strcpy(temp,av[4]); // copied the filename

}

ofstream output("output.txt");
output<<temp;


output.txt had this wierd character: ôæ

commandline argument :camTrackExe 1 1 2010 check.txt
Jul 29, 2010 at 8:03am
Well, there are a few problems with the code:
1. the return type for main is wrong - it must be int.
2. you're not handling the case ac!=5 correctly. It should've been: if (ac!=5)return 1;
3. don't use char arrays when you can avoid it. There is no need to copy the argument to temp, especially since you're not handling the case of the argument's length being >9.
Last edited on Jul 29, 2010 at 8:04am
Jul 29, 2010 at 8:14am
Hi,
The below snippet reads filename from commandline. It creates the file with that filename.
And then finally writes the commandline (which here is same as filename) into that file.

1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<fstream>
using namespace std;

int main(int argc, char* argv[])
{
ofstream out(argv[1]);
out<<argv[1];
return 0;
}


I have checked it,it works fine.
Hope it helps
Last edited on Jul 29, 2010 at 8:15am
Jul 29, 2010 at 4:31pm
Thanks for the help guys
Topic archived. No new replies allowed.