User input after *argv[] input

Hello. I would like to know how to get a file name input into
 
int main(int argc, char *argv[])

Like the user inputs: --file text.txt and a file is made called
text.txt. can anyone tell me how to do this? Thanks.
How to parse command line parameters. - C ++ Articles
http://www.cplusplus.com/articles/DEN36Up4/
[Removed.]
Last edited on
I'll try it.
Why doesn't my code work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <include.h> // iostream, string, ofstream, namespace std

int main(int argc, char *argv[])
#include <include.h> // iostream, string, ofstream, namespace std

int main(int argc, char *argv[])
{
	if(argc < 3)
	{
		cout << "Usage: --file <filename>";
		char *file;
		for(int i = 0; i<argc; ++i)
		{
			if(i + 1 < argc)
			{
				if(string(argv[i]) == "--file")
				{
					file = argv[i++];
					ofstream outFile;
					outFile.open(file);
				}
			}
		}
	}
	return 0;
}


I typed in:
main.exe --file text.txt

Nothing happened. It should make a file called text.txt.

EDIT:

This code worked. Kind of obvious error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <include.h> // iostream, string, ofstream, namespace std

int main(int argc, char *argv[])
{
	if(argc < 3)
	{
		cout << "Usage: --file <filename>";
	}
	char *file;
	for(int i = 0; i<argc; ++i)
	{
		if(i + 1 < argc)
		{
			if(string(argv[i]) == "--file")
			{
				file = argv[2];
				ofstream outFile;
				outFile.open(file);
			}
		}
	}
	return 0;
}

Last edited on
No real need to have a loop for a 3 parameter command line:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[])
{

   if (argc < 3)
   {
      std::cout << "Usage: '--file <filename>'\n";
   }
   else
   {
      if ("--file" != std::string(argv[1]))
      {
         std::cout << "Invalid parameter\n";
      }
      else
      {
         std::ofstream f(argv[2]);
      }
   }
}
Thanks! I'll try this.
the point of flags (-text or --codeword type stuff) is to be able to have them or not have them, and in any order. Something like grep, for example, you can have or not have various flags in whatever order and it works fine because the front end of that program does a bunch of heavy parsing of the arguments.

If you want a rigid order, eg program.exe -f filename -x option such that -x option and -f filename won't work (order reversed) then there isnt any point to having the flags at all! Just say program.exe filename option instead! Then you don't need the extra flag bits at all. If you can have 2 behaviors for one arg, via a flag, then you need it (say you have -f filename or -z filename, and the f/z tells you to DO something different, then you need the flags again).

In windows (and unix!? I forget?) where spaces are allowed in folder names, you have to watch out for that as it is considered to be two arguments and the space itself is lost. Quotes around the arg may work, I am not sure 100% off the top of my head, but watch out for that issue anyway.

Last edited on
Thank you. I'll try to remember all this stuff... I should have learned something easier before c++ I guess. Lol
Topic archived. No new replies allowed.