I have an assignment where I'm not allowed to edit the main program, but to free the memory of a copy of argv. So far the only solution I've found is using argc to determine how many blocks need to be freed. However, argc is not an input into the freeing program, but a copy of argv is. Is there anyway to derive argc from **argv?
This should do the trick:
#include <stdio.h>
int main(int argc, char ** argv){
int count = 0;
for(int i = 0; 1; i++)
if(argv[i] == 0) break;
else count++;
printf("Argc: %d\n", count);
}
Or in a compact way (thanks to chqrlie):
#include <stdio.h>
int main(int argc, char **argv) {
int count;
for (count = 0; argv[count] != NULL; count++)
continue;
printf("Argc: %d\n", count);
}
Related
I want to send the size of an 2D array and filename as command line arguments in C.
int main(int argc,char *argv[])
I know this works for single arguments, but how do I take two arguments?
argc tells you the number of arguments.
argv is an array of char pointers to c-style strings.
So you can simply print all arguments by:
int main(int argc, char *argv[])
{
int i;
for (i=0; i<argc; ++i)
{
printf("%s\n", argv[i]);
}
}
You can use atoi to convert (the initial portion of) a string to an integer.
So you can do something like:
int main(int argc,char *argv[])
{
char filename[100];
int size = 0;
int i;
if (argc < 3)
{
printf("Too few arguments\n");
return 0;
}
if (strlen(argv[1]) >= 100)
{
printf("File name too long\n");
return 0;
}
strcpy(filename, argv[1]);
size = atoi(argv[2]);
if (size <= 0)
{
printf("Invalid size\n");
return 0;
}
....
....
return 0;
}
Note that it is usually not necessary to copy file name arguments to another variable unless you are going to modify the value in some way.
So, I am trying to use argc and argv in Caesars cipher in order to execute the program with just [./ key ;string] (e.g. ./ 13 Caesar). I have tried in lots of ways, although I must admit I am really lost here. I was thinking I should just use main(void) and ask the input with fgets, but I still have some curiosity in: How could I make it work with “int main(int argc, char *argv[])?”. Any clues you can give me?
Thank you for your help. Here is the code with the current outputs:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int key;
int result;
char str[60];
int k = 0;
printf("argc =%d\n", argc);
printf("argv =%d\n", argv);
printf("key =%d\n", key);
if (argc != 2)
{
printf("You didn't enter a key.\n");
return 1;
}
else
{
int k = key % 26;
printf("k =%d\n", k);
if (k == 0)
{
printf("Invalid key.\n");
return 1;
}
}
}
Output:
$ ./ceasar 13
argc =2
argv =-13216
key =0
k =0
Invalid key.
Edit: Tentative Answer
int main(int argc, char* argv[])
{
int i;
int key = atoi(argv[1]);
int result;
char str[60];
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}
if (argc != 2)
{
printf("You didn't enter a key.\n");
return 1;
}
else
{
if (key == 0)
{
printf("Invalid key.\n");
return 1;
}
}
}
Printing argv with the "%d" specifier is undefined behavior, read printf()'s manual and use "%p".
On the other hand, if you want to print the string you should access the appropriate element with array notation. For example
printf("First Argument: %s\n", argv[1]);
this applies to all arguments, noting that argv[0] is the name of the executable as invoked in the command line.
You should also be careful before accessing argv[1] to check that argc > 2, and always check the current argument + !.
So I have a string passed into main function: int main(int argc, char* argv[])
I understand argc (which is 2 in this case), but don't understand how I can read argv[] character by character?
When I print argv[0] shouldn't that print the first character in the array of characters for that string?
Thanks
sample
#include <stdio.h>
int main(int argc, char *argv[]){
int i,j;
for(i=0; i<argc; ++i){
for(j=0; argv[i][j] != '\0'; ++j){
printf("(%c)", argv[i][j]);
}
printf("\n");
}
return 0;
}
One more example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
if(argc != 2)
{
//argv[0] is name of executable
printf("Usage: %s argument\n", argv[0]);
exit(1);
}
else
{
int i;
int length = strlen(argv[1]);
for(i=0;i<length;i++)
{
printf("%c",argv[1][i]);
}
printf("\n");
return 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.
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.
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;
}
I'm working on a C program that get the command line arguments and append to them a file extension.
The execution will be something like this:
>myprogram file1 file2
and will execute another program that will use as argument file1.txt and file2.txt.
I tried doing that would add the extension and run one command (s1 is the path and s2 is argv[i] on a loop:
int getfile(char *s1, char *s2){
char *str2 = malloc(sizeof(s2)+3);
strcpy(str2,s2);
strcat(str2,".txt");
execl(s1,"program",str2,NULL);
exit(0);
}
The function will run the program for one file (>program file1.txt and >program file2.txt), but I will need to find a way to run it this way (>program file1.txt file2.txt).
I tried to modify argv directly, but I was unsuccessful.
Any advise?
Try this code:
int main(int argc, char *argv[])
{
char *buffer;
char command[512];
int i = 1;
for(i = 1; i < argc; i++){
buffer = malloc(strlen(argv[i]) + 5);
strcpy(buffer,argv[i]);
strcat(buffer,".txt");
sprintf(command,"touch %s\0",buffer);
system(command);
free(buffer);
}
return 0;
}
A simple program that has no error checking, and I like to explicitly add the string terminator.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, int *argv[]){
if(argc==1){
printf("You have not entered anything!\n");
return 0;
}
char *arr=malloc(1000*sizeof(char));
int i;
strcat(arr, argv[0]);
strcat(arr, " ");
for(i=0;i<argc-1;i++){
strcat(arr,argv[i+1]);
strcat(arr,".txt");
strcat(arr," ");
strcat(arr,"\0");
}
printf("%s\n",arr);
free(arr);
return 0;
}