Solved

Reading a filename from command line.

Posted on 2003-10-25
4
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.
Suggested Courses

623 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