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;
}
}
Related
I'm just trying to fetch the input from command prompt and want to convert the input to integer.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int num;
if(atoi(argv)==0)
{
printf("Enter the number : ");
scanf("%d",&num);
}
else
{
num = atoi(argv);
}
}
Above is what I've tried, and I don't know where I've went wrong.
For the below code I'm getting the error as showed in the picture. I want to run it in both the way like getting from the command prompt and if there is no input from command prompt the values must be fetched when the program runs.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int num_of_values,sum = 0;
int i;
float avg;
if(atoi(argv[1])==0)
{
printf("Enter the number of values: ");
scanf("%d",&num_of_values);
int* sum_arr = malloc(num_of_values*sizeof(int));
printf("Enter the values: ");
for(i=0;i<num_of_values;i++)
{
scanf("%d",&sum_arr[i]);
sum+=sum_arr[i];
}
}
else
{
for (i = 2; i<=argc; i++) {
sum+=atoi(argv[i-1]);
}
num_of_values = i-2;
}
avg = sum / num_of_values;
printf("Sum and Average is %d and %f", sum,avg);
}
In if(atoi(argv)==0) you probably want to check how many arguments the user gave at the command line. That's what argc is for.
num = atoi(argv); should be num = atoi(argv[1]);
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int num;
if (argc < 2) { // no argument given on cmdline
printf("Enter the number : ");
if (scanf("%d", &num) != 1) { // check that scanf succeeds
puts("Failed reading a number");
return 1;
}
} else {
num = atoi(argv[1]); // dereferencing the second element
}
printf("num=%d\n", num);
}
Just check for correct number of command-line arguments before calling atoi. If not correct, then throw error and return.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Usage: %s <number>\n", argv[0]);
return -1;
}
int num = atoi(argv[1]);
printf("num = %d\n", num);
}
argv is a pointer. argv[i] is a string. argv[0] will be the program name so argv[1] onwards are the cmd line args.
int main(int argc, char **argv)
{
int num;
if(argc<2) /* user has not provided any cmd line args */
{
printf("Enter the number : ");
scanf("%d",&num);
}
else
{
num = atoi(argv[1]);
}
printf("Number = %d\n",num);
}
So I have this code:
#include <stdio.h>
int main(int argc, char **argv) {
//Reassign input arguments into local values
// Check if inputs are valid
// translate the input string
//assign the list into a nested string
//search for translated string in the list
//save all found cases
//print all found cases
int i = 0;
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
printf("%d",argc);
return 0;
}
Which after typing:
outDebug.exe hello <seznam.txt into the command prompt...
it gives me these returns:
argv[0] = outDebug.exe
argv[1] = hello
2
Where did the file go to if it's not in argv?
As mentioned in the comments, you can get it from stdin. With fgets for example:
#include <stdio.h>
int main(int argc, char **argv)
{
int i = 0;
for (i = 0; i < argc; i++)
{
printf("argv[%d] = %s\n", i, argv[i]);
}
printf("%d\n",argc);
char buffer[256];
while(fgets(buffer, 256, stdin)) puts(buffer);
return 0;
}
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);
}
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 + !.
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;
}