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.
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.
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.
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.
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.