0

I'm having trouble trying to figure out how to concatenate the file path with the strings. The user input the strings in the command prompt like this:

    StringLabMain.exe Mary had a little lamb 1234

It supposed to print out something like this:

    Concatenated arguments: d:\Documents and Settings\labadmin\My Documents\Visual Studio 2012\Projects\test\debug\StringLabMain.exeMaryhadalittlelamb1234

but my code prints out this:

    Concatenated arguments: StringLabMain.exeMaryhadalittlelamb1234

Here is my code (I don't understand how the concatenate works to include the file path with the strings):

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

      int i;

      for (i = 0; i < argc; i++)
      {
         printf("%s", argv[i]);

      }
      return 0;
    }

I hope I explained this clearly.

user3768057
  • 43
  • 2
  • 8
  • 1
    Try invoking your program with `d:\Documents and Settings\labadmin\My Documents\Visual Studio 2012\Projects\test\debug\StringLabMain.exe`. (Though you probably need some quotes in there somewhere.) – Hot Licks Jul 09 '14 at 03:02
  • 1
    See http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c – Jim Balter Jul 09 '14 at 03:04
  • (The standard is that `argv` returns exactly the command line parms, with the program name as element zero. So the path will not be there by default.) – Hot Licks Jul 09 '14 at 03:04
  • The 'd:\Documents and Settings\labadmin\My Documents\Visual Studio 2012\Projects\test\debug\' was already there in the command prompt. I dragged the cmd.exe into the debug folder of the project and so it came to this 'd:\Documents and Settings\labadmin\My Documents\Visual Studio 2012\Projects\test\debug\'StringLabMain.exe Mary had a little lamb 1234 – user3768057 Jul 09 '14 at 03:05
  • @HotLicks So how can I include the file path with it? – user3768057 Jul 09 '14 at 03:15
  • @HotLicks I read the C99 standard (section 5.1.2.2.1, Program startup) as simply not specifying whether the path appears. Clearly it would be easiest to simply show the actual path from the command line, but it doesn't seem to specify that. Interestingly, the program name itself doesn't even have to appear! (Obviously, that would be an unusual -- perhaps embedded -- system.) – ooga Jul 09 '14 at 03:15
  • The shell program puts the prefix there. – chux - Reinstate Monica Jul 09 '14 at 03:16
  • "So how can I include the file path with it?" -- Why are you still asking this when I posted the answer 11 minutes earlier? – Jim Balter Jul 09 '14 at 03:46
  • @JimBalter Hello. I don't understand how I can use it in a strcat – user3768057 Jul 09 '14 at 03:48
  • Why don't you print your current directory and then concatenate the names from argv? – Mickey Jul 09 '14 at 04:14
  • @ooga - Yeah, I haven't read a C standard since about 1980. The *old* standard was that the tokens from the command line were placed directly into `argv`, with no modification. But I suppose the OS or C runtime can muck with those values however it wants, if the current standard allows. – Hot Licks Jul 09 '14 at 11:29
  • "So how can I include the file path with it?" Type it on the command line. – Hot Licks Jul 09 '14 at 11:30

2 Answers2

0

The following code demonstrates how to use strcat() to build up a string of all argv[] elements:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
   {
   int     i;
   size_t  outputSize = 1;
   char   *output     = NULL;

   /* Allocate a buffer large enough to hold the string termination character. */
   output=malloc(outputSize);
   if(!output)
      {
      fprintf(stderr, "malloc() failed.\n");
      goto CLEANUP;
      }
   *output = '\0';

   /* Iterate argv[] elements. */
   for(i = 0; i < argc; i++)
      {
      char *tmp;

      /* Increase the size of the output buffer to hold this argv[] element. */
      outputSize += strlen(argv[i]);
      tmp=realloc(output, outputSize);
      if(!tmp)
         {
         fprintf(stderr, "realloc() failed.\n");
         goto CLEANUP;
         }
      output=tmp;          

      /* Concatinate this argv[] element to the output string. */
      strcat(output, argv[i]);
      }

   /* Print the result. */
   printf("%s\n", output);

CLEANUP:

   if(output)
      free(output);

   return 0;
   } 

On Linux, you can also include the path of the current working directory, like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[])
   {
   int     i;
   size_t  outputSize;
   char   *output     = NULL;

   output=getcwd(NULL,0);
   if(!output)
      {
      fprintf(stderr, "getcwd() failed.\n");
      goto CLEANUP;
      }
   outputSize = strlen(output) + 1;

   for(i = 0; i < argc; i++)
      {
      char *tmp;

      outputSize += strlen(argv[i]);
      tmp=realloc(output, outputSize);
      if(!tmp)
         {
         fprintf(stderr, "realloc() failed.\n");
         goto CLEANUP;
         }
      output=tmp;          

      strcat(output, argv[i]);
      }

   printf("%s\n", output);

CLEANUP:

   if(output)
      free(output);

   return 0;
   }  

The above example is Linux specific due to a Linux extension to 'getcwd()'. The Linux getcwd man page states:

As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc(3) if buf is NULL. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. The caller should free(3) the returned buffer.

Apparently, _getcwd() works the same way on MS Windows. MSDN states about _getcwd():

The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) is automatically allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer).

So, perhaps the following (untested) code would be suitable for a MS Windows environment:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <direct.h>

int main(int argc, char *argv[])
   {
   int     i;
   size_t  outputSize;
   char   *output     = NULL;

   output=_getcwd(NULL,0);
   if(!output)
      {
      fprintf(stderr, "_getcwd() failed.\n");
      goto CLEANUP;
      }
   outputSize = strlen(output) + 1;

   for(i = 0; i < argc; i++)
      {
      char *tmp;

      outputSize += strlen(argv[i]);
      tmp=realloc(output, outputSize);
      if(!tmp)
         {
         fprintf(stderr, "realloc() failed.\n");
         goto CLEANUP;
         }
      output=tmp;          

      strcat(output, argv[i]);
      }

   printf("%s\n", output);

CLEANUP:

   if(output)
      free(output);

   return 0;
   }  
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
0

First, if your only purpose is to print the directory and the concatenated args, so you just have to print the current directory before the main loop. This may be done using getcwd().

#include <stdio.h>
#include <unistd.h>

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

  int i;
  printf("%s", getcwd(0,0));
  for (i = 0; i < argc; i++)
  {
     printf("%s", argv[i]);
  }
  return 0;
}

But for more general purposes I really recommend you to use stracat() which concatenates string. So you have to declare a "string" (using char *) with the current working directory, and then concatenate the args. This will be done like this way:

#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  char* toPrint;
  int i;
  toPrint = getcwd(0,0);
  for (i = 0; i < argc; i++)
    strcat (toPrint, argv[i]);
  printf("%s\n",toPrint);
  return 0;
} 

I hope that know it's clear.

Mickey
  • 480
  • 4
  • 13