I have a simple program "prog.c" in C:
#include<stdio.h>
int main(int argc, char *argv[]) {
printf("Count: %d\n", argc);
printf("Text: %s\n", argv[1]);
return 0;
}
When I compile the program and run with a .txt file as parameter, the program does not recognize this parameter at all.
I compile with: gcc prog.c
which creates an "a.exe" file.
Then I run the .exe program with .txt file as argument (tried all options below):
a < text.txt
a.exe < text.txt
The output is always
Count: 1
Text: (null)
I am running everything on Win10 and using MinGW for compilation.
You want to check that argv[1] is set before referencing it with:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Count: %d\n", argc);
if(argc > 1) {
printf("Text: %s\n", argv[1]);
}
}
Then you would run it like this:
./your_program argument
Count: 2
Text: argument
When you use < test.txt the file test.txt is copied to standard input of the program. You would use, for example, read() or fread() to read said input.
Related
In netbeans I need to use command line input redirect to make my program scanf a file.
my command line args look like
"${OUTPUT_PATH}" < file.txt
file.txt has the word "Hello" in it
my code looks like:
int main(int argc, char** argv) {
char str[30];
scanf("%s", str);
printf("input: %s\n", str);
return (EXIT_SUCCESS);
}
When I run the program, it compiles fine, but doesn't read in the "Hello"; it appears to be looking for input eternally. This is C, not java
I have a working program that computes the cube of a number in a compute_cube.c file. It compiles into compute_cube.
Now I would like to run it through the terminal like this:
./compute_cube 3
And then the terminal would show my program's result (27).
How would I go about doing this? What should I be reading up on?
Use C language's argc and argv:
int main(int argc, char **argv)
{
if (argc > 1)
printf("%s", argv[1]);
}
I know its already been answered but... argc of course = 0-based number of command line args including program name at index 0 and argv contains actual command line text.
#include <stdio.h>
int main(int argc, char **argv) {
if (argc > 1) {
int n = atoi(argv[1]);
printf ("%d^3 = %d\n", n, n*n*n);
return 0;
}
else printf("Usage: %s <num>\n", argv[0]);
return 1;
}
An example of what the program should do:
./executable ls -l
should do the same as
ls -l
so basically it's a useless interpreter for shell commands.
I've created a string fullpath that contains /bin/commandname and a two dimensional array arguments, which is basically argv but without the first two entries (which are executable and commandname).
For the ./executable ls -l example it actually returns the following:
full path: /bin/ls
Argument 0: -l
which is correct, but execv does literally nothing.
How do I fix this?
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char** argv){
char path[6] = "/bin/";
char* fullpath = (char*)malloc(strlen(path)+strlen(argv[1]));
strcat(fullpath, path);
strcat(fullpath, argv[1]);
printf("full path: %s\n", fullpath);
char *arguments[argc-1];
int i;
for(i=0; i<argc-2; i++){
arguments[i] = (char*)malloc(strlen(argv[i+2]));
strcpy(arguments[i], argv[i+2]);
printf("Argument %d: %s\n", i, argv[i+2]);
}
execv("/bin/ls", arguments);
return 0;
}
Your program seems functional when we modify the following line :
char *arguments[argc-1];
Your char **arguments have to be like this line :
char *arguments[] = { "/bin/ls", "-l", NULL };
In other words, don't forget to indicate the command first; and be sure to put NULL at the end of your char **.
Good luck ! ;)
I am very new to C programming, and have written this C program that takes in an input N, and gives a list of all the numbers up to N that are exactly divisible by 7. The program I have written is as follows;
# include <stdio.h>
int main(){
int c,n,k;
int i=0;
int AnswerList [1000];
printf("Enter the number\n");
scanf("%d", &n);
for (c=1;c<=n;c++){
if(c%7==0){
AnswerList[i]=c;
i++;
}
}
for (k=0;k<=i;k++){
printf("%d\n", AnswerList[k]);
}
return 0;
}
I need my program to run such that if N equals 27, I should be able to type into the command line
./byseven 27
In other words, I need to write code that bypasses the printf line I think. I would appreciate any help.
Thanks a lot.
Use command-line arguments. A simple example:
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s N\n", argv[0]);
return 0;
}
int N = atoi(argv[1]); // atoi is used to convert a string to an int
// your code
}
You should use int main(int argc, char** argv) definition. Then argc will be number of your params (first param is always the name of your program), and argv is array of string which contains that params. And scanf function is not needed therefore.
gcc -o hello hello.c
It will compile and produced an exectuable file called hello. To run program type:
./hello
can anyone point me to the problem over here? This compiles but it won't print anything. I need to compare the string from command line argument with the string "hello".
Thanks!
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc == 0)
{
printf("No arguments passed!\n");
}
char *str = argv[1];
if(strcmp("hello", str)==0)
{
printf("Yes, I find it");
}
else
{
printf("nothing");
}
return 0;
}
My ESP suggests that you're running this in an interactive editor/debugger, such as Microsoft Studio. You probably haven't configured the environment to pass any command-line parameters, so you expect to see nothing as your output.
However, you access argv[1], which does not exist, creating a seg-fault, and the program aborts before there is any output.
To fix this, check the value of argc first, and make sure you don't access invalid memory.
Also, I recommend putting a \n at the end of each printf to help flush any buffered output to the console.
int main(int argc, char *argv[])
{
if (argc == 0)
{
printf("No arguments passed!\n");
}
else if(strcmp("hello", argv[1])==0)
{
printf("Yes, I find it\n");
}
else
{
printf("nothing\n");
}
return 0;
}
When you run this, you should see:
$prompt: myprogram
No arguments passed!
$prompt: myprogram hello
Yes, I find it
$prompt: myprogram world
nothing
The problem is the command you're using to run it. As you commented:
i run program > test hello or > test hi and the output is nothing
The > is redirecting output, and ends up giving you no command line arguments. What you want is simply program hello without the output redirection.
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc < 2 || 0 != strcmp("hello", argv[1]))
printf("nothing\n");
else
printf("yes, found it\n");
return 0;
}
and output
bash-3.2$ gcc 1.c -o 1
bash-3.2$ ./1 hello1
nothing
bash-3.2$ ./1 hello
yes, found it
bash-3.2$ ./1
nothing
Try calling your program something different to 'test'