Solved

Reading a filename from command line.

Posted on 2003-10-25
4
243 Views
Last Modified: 2010-04-01
I was wondering what is the correct way to read a filename from the command line?
Note: Not by prompting for and reading in a string.

Thanks
0
Comment
Question by:killer455
4 Comments
 
LVL 1

Accepted Solution

by:
marcin1 earned 20 total points
ID: 9621488
Here is the example how to handle arguments from the command line:

#include <string.h>
int main(int argc, char* argv[])
{
        if (argc > 1)
        {
                //here you can use filename
                printf("argument 1: %s", argv[1]);
        }
        return 0;
}

argc - number of arguments (including the name of the program)
argv - array containing the arguments

argv[0] is the name of your program

Now try to compile it and run with parameters
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9621507
It can be done in the same way as in C


Your main function should look like this:

int main(int argc, char** argv)
{
   /* Use argc and argv as needed */
}

argc contains the number of command lines arguments + 1 (the first argument is the name of your executable).
argv is an array of strings (char pointers) - each string is a command line argument.

Suppose you call "Program.exe filename.txt"

argc = 2
argv[0] is "program.exe"
argv[1] is "filename.txt"
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9621508
For example, if your executable is a.out
$ a.out infile.txt
then argv[0] would contain the executable name (possibly including path - but this is not certain)
and argv[1] is the char array "infile.txt"
so basically if you want to open this file:
ifstream in(argv[1]);
is what you can write (or call the open() function).

Notice that if you have included the path in the file name, all backslashes must be doubled:
Not A:\MyFolder\infile.txt but A:\\MyFolder\\infile.txt
(since the compiler will confuse \M and \i as predefined tags (like \n, \t etc.)

In MS-DOS, the symbol used is "/" so you wouldn't have to worry about that.
0
 

Author Comment

by:killer455
ID: 9624276
Thanks for the help guys.  I guess you cant split only 20 points? i didnt see the "split" button.  Well look for more questions, i got a big (in my rookie opinion) project im working on.  :)

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question