When I compile this using GCC on linux, as I am waiting for input, the "hi" shows up. I do not want to use scanf, and want to know why the hi is showing while I am asking the user to input the name. Also when I want to printout the name of the file that was just passed, I get garbage characters. In netbeans, i get what I want. but on linux, it decides to act weirdly. please help
Code:
int main(int argc, char** argv)
{
char val[70];
if(write(1, "Please input your name", 36)!=36)
{
return -1;
}
if(read(0, val, 36) < 0)
{}
if(write(1, val, 36)!=36)
{}
printf("Yo");//THIS IS PRINTING OUT WAY BEFORE IT IS CALLED, ANY VARIABLE WITH A STRING GETS PRINTED OUT, EVEN WITHOUT PRINTF BEING INVOKED
}
output:
Please input the file nameYo: hi
hi
???Om?0?a?Sm? <<WHAT IS THIS? I DONT GET THIS ON NETBEANS
The third argument to write is the byte length of the string you're trying to print. You have 36, but the string you provide is only 22 bytes long. Changing the code to look like the following will behave as you expected it to:
int main(int argc, char** argv)
{
char val[70];
if(write(1, "Please input your name", 22)!=22)
{
return -1;
}
if(read(0, val, 36) < 0)
{}
if(write(1, val, 36)!=36)
{}
printf("Yo");//THIS IS PRINTING OUT WAY BEFORE IT IS CALLED, ANY VARIABLE WITH A STRING GETS PRINTED OUT, EVEN WITHOUT PRINTF BEING INVOKED
}
Note that you should probably look into using printf and scanf so that you wont have to worry about byte lengths so much.
That might look like this:
#include <stdio.h>
int main(int argc, char** argv)
{
char val[70];
printf("Please input your name");
if(scanf("%69s", &val) == 1)
printf(val);
printf("Yo");
}
Related
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv[])
{
if (argc != 2)
{
printf("Incorrect number of arguments, try again");
return 1;
}
else{printf("Congrats\n");}
printf("argv is: %s",argv[1]);
}
The code is incomplete and the task is for me to receive an input from the user via command line argument and to encrypt it. However I cannot seem to get the correct comman line argument. For example if input is "ceaser.exe 2" where ceaser is the name of my executable file and 2 is my input, argv[1] shows as "2ceaser.execeaser.exe".
Have watched a lot of vids but seems like theres nothing wrong with the format of my code but can't seem to solve this issue. The output I want for argv[1] should be "2" but can't seem to get it.
You declared your main function incorrectly. argv should be of type char** or char*[], not char**[].
int main(int argc, char* argv[])
#include <stdio.h>
#include <stdlib.h>
#define BUFFERSIZE 10
int main(int argc, char *argv[])
{
char address[BUFFERSIZE];
//checking text file on stdin which does not work
if (fgets(address, BUFFERSIZE, stdin) < 42)
{
fprintf(stderr, "The program needs at least 42 addresses for proper functionality.");
}
//while reads the redirected file line by line and print the content line by line
while(fgets(address, BUFFERSIZE, stdin) != NULL)
{
printf("%s", address);
}
return 0;
}
Hi, this is my code. Does not work. The problem is that I have a redirected external file adresy.txt into stdin and I need to check if the file has the required number of rows.
The minimum number of rows that a file must have is 42. If it has 42 or more rows the program can continue, if not, it throws out the fprintf(stderr, "The program needs at least 42 addresses for proper functionality.");
I tried it this way if (fgets(address, BUFFERSIZE, stdin) < 42) but it still tells me that I can not compare pointer and integer
like so: warning: comparison between pointer and integer
In the code extension I will compare the arguments from the user to what is in adresy.txt therefore I need argc and *argv [] but now i need to solve this.
Any advice how to fix it? Thanks for any help.
There are several problems in your code:
#define BUFFERSIZE 10 is odd as your lines but be at least 42 long.
you compare the pointer returned by fgets to 42 which is nonsense, BTW your compiler warned you.
With your method you actually display only one line out of two
You probably want this:
#define BUFFERSIZE 200 // maximum length of one line
int main(int argc, char *argv[])
{
char address[BUFFERSIZE];
while(fgets(address, BUFFERSIZE, stdin) != NULL)
{
// here the line has been read
if (strlen(address) < 42)
{
// if the length of the string read is < 42, inform user and stop
fprintf(stderr, "The program needs at least 42 addresses for proper functionality.");
exit(1);
}
// otherwise print line
printf("%s", address);
}
return 0;
}
I am currently working on the common return to stack project and I have seen many of example online and on youtube that are very common. Yet when I try to recreate the project with the int2hexstr() function my results are not the same.
Example of test string arugment
As you can see 76 A's print to the screen along with 100 I's and 4 B's along with the shellcode.
However when I replace the BBBB with int2hextr(), My argument is simply exactly what I typed in the single quotation marks.
Example of test string argument with address
I am aware that my results do not print in hexadecimal like most examples in either picture but will that make a difference, as well.
Is there an alternative to use instead of int2hexstr or is there something else I need to type. Also I using a virtual machine, Debian 7.xxx
copy of code i am trying to use return 2 stack
char* dovuln(coonst char*s)
{ char buf[64];
strcpy(buf, s);
puts(buf);
fflush(stdout);
return strdup(buf);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
puts("missing argument");
exit(1);
}
dovuln(argv[1]);
return 0;
}
I'm having a slight issue with my current piece of code and trying to check that arguments have been passed to enum before trying to continue with the program;
enum arg {argName, sineArg, sampleArg, argC}eargs;
int main(int argc, char *argv[]){
long samplingRate = atol(argv[sampleArg]);
float sineFreq = atof(argv[sineArg]);
if (argC < eargs ){
printf("Usage: sineFreq\tsamplingRate\n");
}else{}
}
The code compiles fine, although when run without arguments the program returns "Segmentation fault: 11" instead of the usage message that I want to print to console.
you have to do the check before accessing argv[...]
EDIT: the check itself is wrong too (eargs is undefined; you should write
int main(int argc, char *argv[]) {
if (argc < argC) /* --> you should change you naming conventions! */
return EXIT_FAILURE;
long samplingRate = atol(argv[sampleArg]);
Your usage check is against eargs that has nothing to with the number of command line arguments passed. No matter how many arguments you pass, eargs is always going to be 0.
Also you should do the check before using any of the command line arguments. Place the condition at the start of main():
#include<stdio.h>
#include <stdlib.h>
enum arg {argName, sineArg, sampleArg, argC}eargs;
int main(int argc, char *argv[]) {
/* if ( argc < 3) would be explicit and clear here */
if (argc < argC ) { /* You expect at least two arguments */
printf("Usage: sineFreq\tsamplingRate\n");
return 1; // return from main on failure
}
long samplingRate = atol(argv[sampleArg]);
float sineFreq = atof(argv[sineArg]);
....
}
I have also included standard headers as you are using printf() and atol()
you need to change argC to argc, in the if statement...c is case-sensitive!!!
When you run the code without arguments, this
long samplingRate = atol(argv[sampleArg]);
accesses argv[2], which is undefined behavior, since you don't provide enough args (there are only argv[0] = program name and argv[1] = NULL indicating the end of the argv[]).
I'm new to C and I'd like to ask about running a C program and supplying input at the same time.
What I would like to do is run a program (ex. fileOpener) and also state which file to open
./fileOpener < filename1
I've tried it already and it works fine, but what do I use to know what filename1 is? That way I can open the file with
fp = fopen(filename1, "r")
Thanks.
Edit: OK, I'll try to explain a bit more. If there wasn't a "<" then I could just use command line arguments as I have done before, but when I tried it with the <, it didn't work
Specifically: fileOpener code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
printf("%s", argv[1]);
}
when I use ./fileOpener < filename1 the output is ./fileOpener
I used gcc -o fileOpener fileOpener.c as the compiler
int main(int argc, char *argv[])
You can name them whatever you want, but these are the normal names.
argc is non-negative. It gives the number of useful elements in argv.
If argc is positive, argv[0] contains the program name. Then argv[1] through argv[argc - 1] point to character arrays that contain the program's command line arguments.
For example, if I run a program at the command line, such as
unzip filename.zip
argc will equal 2; and argv[0] will compare equal to "unzip"; and argv[1] will compare equal to "filename.zip".
Source
You can't do that, if you use redirection (i.e. "< filename") the file is opened by the system. You could discover the name, but it's non-portable, and anyway useless since the file is already open. Just use stdin instead of fp, and you need not use fopen() (nor fclose()):
int main()
{
char buffer[1024];
// fgets() reads at most 1024 characters unless it hits a newline first
// STDIN has been already opened by the system, and assigned to data flowing
// in from our file ( < inputFile ).
fgets(buffer, 1024, stdin);
printf("The first line of input was: %s", buffer);
}
A different approach is to use arguments:
int main(int argc, char **argv)
{
FILE *fp = NULL;
char buffer[1024];
if (argc != 2)
{
fprintf(stderr, "You need to specify one argument, and only one\n");
fprintf(stderr, "Example: %s filename\n", argv[0]);
// Except that argv[0], this program's name, counts.
// So 1 argument in command line means argc = 2.
return -1;
}
printf("I am %s. You wanted to open %s\n", argv[0], argv[1]);
fp = fopen(argv[1], "r");
fgets(buffer, 1024, stdin);
printf("The first line of input was: %s", buffer);
fclose(fp); fp = NULL; // paranoid check
return 0;
}
You need setup your program to take a command line argument. Here's a good tutorial that solves your exact question:
http://www.cprogramming.com/tutorial/c/lesson14.html
A program's main function in C has two arguments:
int main(int nArgs, char *pszArgs[]) {}
That first argument tells the program how many parameters were passed onto the program when you ran it. Usually, this will just be 1, because it includes the program's name.
The second argument is a table of strings, which can be accessed thus (the program below prints the parameters given to it):
int main(int nArgs, char *pszArgs[])
{
int i = 0;
while (i < nArgs)
{
printf("param %d: %s\n", i, pszArgs[i]);
i++;
}
return 0;
}