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;
}
Related
This program is supposed to open a text file, then search for given words in argv. It will search for the words line by line and if it finds one of the given words in that line the program should print it.
This the code I wrote for it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int existe_mot_cle(char s[1024], int argc, char *argv[])
{
int test = 0;
for (int i = 2; i < argc; i++)
{
if (strstr(s, argv[i]))
test = 1;
break;
}
return test;
}
int open_file(char *argv[], FILE *fp)
{
fp = fopen(argv[1], "a");
}
int main(int argc, char *argv[])
{
FILE *fp;
char s[1024];
if (!open_file(argv, fp))
return 0;
while (fgets(s, 1024, fp))
{
if (existe_mot_cle(s, argc, argv))
printf("%s", s);
}
fclose(fp);
}
The problem is when I run it, nothing happens and I don't know why. I am new to the C language. Can someone give me the solution and explain it please?
You are breaking the for loop right after the first if statement is executed. You should surround it with curly braces:
int existe_mot_cle(char s[1024], int argc, char *argv[])
{
int test = 0;
for (int i = 2; i < argc; i++)
{
if (strstr(s, argv[i])) {
test = 1;
break;
}
}
return test;
}
You can make it simpler and more generic:
bool existe_mot_cle(char s[1024], size_t size, const char *ss[])
{
for (size_t i = 0; i < size; i++) {
if (strstr(s, ss[i]))
return true;
}
return false;
}
Also, your open_file() should return an int, but it is not returning anything. Better remove it from your code since it serves no purpose:
int main(int argc, const char *argv[])
{
if (argc < 3) {
printf("Usage: %s [file] [words]\n", argv[0]);
return 0;
}
const char *filename = argv[1]; // More meaningful
const char **otherarg = argv + 2;
FILE *fp = fopen(filename, "r");
if (!fp) {
printf("Could not open %s.\n", filename);
return 0;
}
char s[1024];
while (fgets(s, sizeof s, fp))
{
if (existe_mot_cle(s, argc-2, otherarg)) // I'm using the second "simpler" version
printf("%s", s);
}
fclose(fp);
}
This question already has answers here:
Command-line Parameters in C program?
(5 answers)
Closed 6 years ago.
My question is how to write a fuction with which I will can run a program from console with following parameters:
program.exe -i input.txt -o output.txt -t 1/2/3
#include <stdio.h>
#include <stdlib.h>
char substitute(char letter, char* cipher)
{
int i;
int cipher_length = strlen(cipher);
char substitution = letter;
for(i = 0; i < cipher_length; ++i)
{
if(cipher[i] == substitution)
{
substitution = cipher[(i%2) ? (i-1) : (i+1)];
break;
}
}
return substitution;
}
int main()
{
char c;
int t;
FILE *plik = fopen( "input.txt", "rt" );
FILE *encoded=fopen("output.txt","wt");
char* cipher1 = "GADERYPOLUKIgaderypoluki";
char* cipher2 = "POLITYKARENUpolitykarenu";
char* cipher3 = "KACEMINUTOWYkaceminutowy";
printf("Choose the cipher\n");
printf("[1]GA-DE-RY-PO-LU-KI\n");
printf("[2]PO-LI-TY-KA-RE-NU\n");
printf("[3]KA-CE-MI-NU-TO-WY\n");
scanf("%d",&t);
while(c != EOF)
{
c = getc( plik );
switch(t)
{
case 1:
putc(putchar(substitute(c, cipher1)),encoded);
break;
case 2: putc(putchar(substitute(c, cipher2)),encoded);
break;
case 3: putc(putchar(substitute(c, cipher3)),encoded);
break;
}
}
fclose( plik );
fclose(encoded);
}
I was given sth like this, but I don't know how to use it:
int function(int argc, char*argcv[])
{
int i;
char *string,*input,*output;
for(i=0; i<argc; i++)
{
}
return 0;
}
The parameter int argc contains the number of parameters in the command line.
The parameter char *argv[] is an array with all the strings typed in the command line.
This way, you can retrieve the command line parameters as:
int main(int argc, char *argv[]) {
int i = 0;
for (i = 0; i < argc; i++) {
printf("parameter %d = %s\n", i, argv[i]);
}
}
You can handle then the parameters passed in the command line:
int main(int argc, char *argv[]) {
int i = 0;
for (i = 0; i < argc; i++) {
...
if (strcmp(argv[i], "-i") == 0) {
doSomething(argv[i+1]);
}
...
}
}
To break the string you can use the following code:
// ...
char buffer[256];
char *p1 = NULL;
char *p2 = NULL;
int value = 0;
char old = 0;
strcpy(buffer, argv[7]);
p1 = buffer;
p2 = buffer;
do {
// parse until the delimiter
while (*p1 != '/' && *p1 != '\0') {
p1++;
}
// save delimiter value
old = *p1;
// convert the string to int
*p1 = '\0';
value = atoi(p2);
p1++;
// do something with the value
printf(">> value %d\n", value);
// goto the next token
p2 = p1;
} while (old != '\0');
How can I access piped parameter in c code?
test.c
int main( int argc, char *argv[] ) {
int i = 0;
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
}
Bash:
cat file.txt | ./test
It prints just first argument argv[0] = ./test. How can I access content of file.txt inside c code (as parameter)?
With the pipe, your program gets the content of file.txt in its stdin. So, read from stdin. For example you can use fgets() to read line by line:
#include <stdio.h>
int main(int argc, char *argv[]) {
int i = 0;
char line[1024];
while(fgets(line, sizeof line, stdin)) {
printf("%s", line);
}
}
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 keep getting a segmentation fault with this code that I am trying to get to print the first 6 words of the dictionary. I'm pretty sure I'm using fscanf incorrectly but I'm not sure how/why...
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
int main(int argc, char* enc[])
{
if (argc != 2)
{
printf("Improper command-line arguments\n");
return 1;
}
FILE *Dict;
Dict = fopen("/usr/share/dict/words", "r");
if (Dict == NULL)
{
printf("Could not open dictionary");
exit(1);
}
char* full = enc[1];
char* salt[2];
for (int i=0; i<2; i++)
{
salt[i] = &full[i];
}
char* key[50];
for (int i=0; i<6; i++)
{
fscanf(Dict, "%s", *key);
printf("%s", *key);
}
}
C strings are either a character array: char name[10], or pointer-to-char (which points to a valid range of memory): char* name.
What you have here is an array of 50 pointers to characters (or strings):
char* key[50];
for (int i=0; i<6; i++)
{
fscanf(Dict, "%s", *key);
printf("%s", *key);
}
key is probably intended to be a 50-character C string buffer:
char key[50];
for (int i=0; i<6; i++)
{
fscanf(Dict, "%s", key);
printf("%s", key);
}