[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Storing argv as Variable

How can I pass the contents of argv into a variable?

For instance, I want the user to use a filename as the arg...  so argv would be equal to file.txt.  I want to take that "file.txt" and store it as the variable "infile".
0
tsurai
Asked:
tsurai
4 Solutions
 
Anthony2000Commented:

you can do the following:

int main(int argc, char * argv[])
{
   char fileName[128];

   if(argc >= 2)
   {
      strncpy(fileName, argv[1], sizeof(fileName)-1);
      fileName[sizeof(fileName)-1] = 0
   }
   else
   {
        // print message telling user to add in the filename
   }



0
 
Anthony2000Commented:
I should have used infile instead of fileName. Sorry.
0
 
Anthony2000Commented:
To be complete:

#include <stdio.h>

int main(int argc, char * argv[])
{
   char infile[128]; // I usually use the MAX_PATH or PATH_MAX here instead of a literal constant

   if(argc >= 2)
   {
      strncpy(infile, argv[1], sizeof(infile)-1);
      infile[sizeof(infile)-1] = 0
   }
   else
   {
        // print message telling user to add in the filename
   }
 
   return 0;
}
0
Technology Partners: 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!

 
Infinity08Commented:
       #include <stdio.h>
       
        int main(int argc, char **argv) {
            char *infile = "";
            if (argc > 1) {
                infile = (char*) calloc(strlen(argv[1]) + 1, sizeof(char));
                strcpy(infile, argv[1]);
            }
            printf("the filename : %s\n", infile);
            return 0;
        }


compile and run like this :

        app file.txt

and the output should be :

        the filename : file.txt
0
 
Infinity08Commented:
And, of course, if you intend to do more with the application, you need to add a free(infile) as soon as you don't need the filename any more ;)
0
 
jkrCommented:
Or:

#include <string.h>

int main(int argc, char * argv[]) {

  char* pInFile = NULL;

  if (argc != 2) return -1; // invalid parameters

  pInFile = strdup(argv[1]); // duplicate input argument

  //
  // your code goes here...
  //

  free(pInFile); // release memory

  return 0;
}
0
 
ozoCommented:
Will you be wanting to anything with the variable that you couldn't have done with
#define infile argv[1]
0
 
itsmeandnobodyelseCommented:
>>>> Will you be wanting to anything with the variable that
>>>> you couldn't have done with
ozo is right. I can't see any value in copying an already existing char array.

#define infile argv[1]
That seems to me dispensable as well. If you want to open the file simply do

#include <fstream.h>

int main(int nargs, char * szargs[])
{
   FILE * pinfile = NULL;
   if (nargs < 2)
   {
          printf("usage: %s <inputfile>\n\n", szargs[0]);
          return 1;  
   }
   pinfile = fopen(szargs[1], "r"); /* at windows: "rt" */
   ....
   return 0;
}


Regards, Alex
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now