Solved

Reading a filename from command line.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 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

8 Experts available now in Live!

Get 1:1 Help Now