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
Solved

Reading a filename from command line.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 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