Solved

Reading a filename from command line.

Posted on 2003-10-25
4
238 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now