I am trying to write an c program which get two float numbers from user and then calls another program with execv() command. But I can't do it , because of converting float to char or I don't know why.
The problem is execv() command is not working; the output must be like that
Enter first num: 5 Enter second num: 7
5.000000 + 7.000000 = 12.000000 parentPID: 9745 childPID: 9746 works now
but it is is like that now
Enter first num: 5 Enter second num: 7 parentPID: 9753
childPID: 9754 works now
my first c program sum.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
if(argc!=3)
printf("error...\n");
double a=atof(argv[1]);
double b=atof(argv[2]);
printf("%lf + %lf = %lf \n",a,b,a+b);
return 0;
}
and the second program calculate.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
float x,y;
pid_t pid;
printf("Enter first num: ");
scanf("%f",&x);
printf("Enter second num: ");
scanf("%f",&y);
if((pid=fork())== -1)
{
printf("can not fork..\n");
exit(1);
}
if(pid==0) //child
{
pid=getpid();
char *temp[] = {NULL,NULL,NULL,NULL};
temp[0]="sum";
sprintf(*temp[1],"%f",x); //here I want to convert float number to char but it doesn't work
sprintf(*temp[2],"%f",y);
execv("sum",temp);
}
else
{
wait(NULL);
printf("parentPID: %d childPID: %d works now.\n", getpid(), pid);
}
return 0;
}
char command1[50], command2[50]; // Added
char *temp[] = {NULL, command1, command2, NULL}; // Modified
temp[0]="sum";
sprintf(temp[1],"%f",x); // remove *
sprintf(temp[2],"%f",y); // remove *
You are not allocating to temp[1] and temp[2] and using those as destination buffer in sprintf and using incorrect * in sprint.
You can use malloc to allocate this memory or use other string as shown in above example to initialize the array.
From kind comment of Sourav Ghosh:
In sum.c, change below lines of code to:
if(argc!=3)
{
printf("error...\n");
return -1;
}
or else, it may lead to undefined behaviour.
Related
I have to make simple shell which read commands and execute them in order. condition is not changing the form of main function, and execute function should be recursive.
main problem is that it seems waitpid doesn't work. but I know, there are so many problem in my code. please let me know where I should start from..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX 10
char cmmd[MAX][256];
int sp;
char *argv[10];
int size;
void ClearLineFromReadBuffer(void){
while(getchar() != '\n');
}
void printCommands(){
size = sp+1;
//print by moving stack pointer
while(1){
if (sp==-1) break;
printf("Command line : %s\n", cmmd[sp]);
sp--;
}
printf("print end\n");
}
void readCommandLines(){
int a = 0; //return of scanf
while (1){ //write commends to cmmd untill get ctrl+d
printf(">");
a = (scanf("%[^\n]s", cmmd[sp])); //take input to str untill get enter(scanf returns -1)
if (a==-1) {sp--; break;}
if (a==1) ClearLineFromReadBuffer();
if (a==0) {printf("error"); break;}
sp++;
}
printf("\n");
}
void readACommand(char *line){ //line takes string's name.
int i=0;
argv[i]=strtok(line," "); //i==0
while(strtok(line," ")!=NULL){
i++;
argv[i]=strtok(NULL," ");
}
printf("%s",argv[0]);
printf("%s",argv[1]);
}
void executeCommands(){ //Recursive function
int n = sp;
n++;
printf("%d",n);
printf("%s",cmmd[n]);
char *cmd_line = cmmd[n]; //command line which child process will execute
unsigned int child_pid; //fork() returns process id of child in parents process
int status; //status takes return of child's exit()
child_pid=fork();
if (child_pid != 0){ // Parents process
printf("parents access");
waitpid(child_pid,&status,0);
printf("***Process %d Child process %d DONE with status %x\n\n",getpid(),child_pid,status);
sp++;
if(sp<size)
executeCommands();
}
else if (child_pid == 0){ //fork() returns 0 in child process
printf("***Process %d Executing Command %s",getpid(),cmd_line);
readACommand(cmmd[n]);
execve(argv[0],argv,NULL);
printf("ERROR - not executing command \"%s\"\n",argv[0]); //can be printed because exec() failed
}
}
int main(){
readCommandLines();
printCommands();
executeCommands();
return(0);
}
this is outcome.
enter image description here
the way you're tokenizing the string is very wrong. There's a lot of strtok calls, and your loop can be an infinite loop since you're calling strtok in the loop with the initialization string, not NULL
Plus you're not setting to NULL after the last argument, which is required by execv to know when the arguments have run out (no size is passed)
Here's a standalone example and a proper readACommand routine:
#include <stdio.h>
#include <string.h>
char *argv[100];
void readACommand(char *line){ //line takes string's name.
int i=0;
argv[i]=strtok(line," "); //i==0
while(argv[i]!=NULL){
argv[++i]=strtok(NULL," ");
}
}
int main()
{
char line[] = "this is a command";
char **ptr=argv;
readACommand(line);
while(*ptr != NULL)
{
printf("Arg: %s\n",*ptr);
ptr++;
}
return 0;
}
executing (detecting the NULL pointer in the end):
Arg: this
Arg: is
Arg: a
Arg: command
What I am trying to do:
Make a program that opens a file and read the content (mostly characters)
Example on what it should do:
The file contains "ABA", It should print: Letter A Count 2 || Letter B Count 1
but I am getting this error when I start debugging http://imgur.com/a/zvpWg
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct arr
{
char letter;
int count;
};
void main ()
{
arr s[7];
char letter;
FILE *fp;
fp=fopen("D:\\data.txt","r");
if(fp==NULL)
{
printf("Error File Not Found\n");
getch();
exit(1);
}
while(fscanf(fp,"%ch",&letter)!=EOF)
{
for(int i=0;i<7;i++)
{
if(!isalpha(s[i].letter))
{
s[i].letter=letter;
s[i].count=1;
break;
}
else if(s[i].letter == letter)
s[i].count++;
break;
}
}
for(int h=0;h<7;h++)
printf("Letter: %c Count: %d ||",s[h].letter,s[h].count);
getch();
}
Where am i wrong exactly ?
The Problem might be -
function isalpha() is being called with an integer whose value is not a single byte i.e. 0-255.
How do you get input using prompt? I tried compiling the code bellow into "a.exe" and executing it from CMD like "gcc a.exe 5", but it doesn't output the number like it was supposed to.
#include <stdio.h>
int main(int a)
{
printf("%d\n", a);
return 1;
}
Have I done anything wrong when installing the compiler or am I trying to run it wrong?
Your main() parameters are wrong, you should do it this way:
int main(int argc, char **argv) {
if(argc > 2) {
printf("%s\n", argv[2]);
}
else {
printf("No arguments\n");
}
}
Note that int argc represents the number of parameters and char **argv is an array containing all the parameters, as strings, including "gcc", "a.exe", etc.
In your case, if you run your program this way: gcc a.exe 5, your parameters would be: argc = 3, argv = ["gcc", "a.exe", "5"]
To get input using the prompt, the easiest way would simply be to use a scanf statement. scanf basically waits for, and scans user input, which can then be stored as a variable. For example, a code that would take input for "Give me a number." and then spits back the result would be:
#include <stdio.h>
int main()
{
int num; //Initializes variable
printf("Please give me a number.\n"); //Asks for input
scanf("%d", &num); //scanf is the function, %d reserves the space, and the &*variable* sets the input equal to the variable.
getchar(); //Waits for user to input.
printf("Your number was %d.\n", num); //Spits it back out.
return 0;
}
The output would be:
[PROGRAM BEGINS]
Please give me a number.
>>>5
Your number was 5.
[PROGRAM ENDS]
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc == 2)
printf("%d\n", atoi(argv[1]));
return 0;
}
#include <stdio.h>
int main()
{
int m,n; scanf("%d %d",&m,&n);
char ar[m][n];
char buf[n];
int a,b;
for(a=0;a<m;a++)
{
gets(buf);
for(b=0;b<n;b++) ar[a][b] = buf[b];
}
for(a=0;a<m;a++,printf("\n")) for(b=0;b<n;b++) printf("%c",ar[a][b]);
return 0;
}
This code takes m lines as input from stdin, each line containing n characters, and prints all the lines to stdout. Simple as that. But there seems to be a memory leak, because the first time gets(buf) is encountered, its execution is skipped.
I tried it in C++ too, thinking the memory leak will disappear. Here is the code:
#include <cstdio>
using namespace std;
int main()
{
int m,n; scanf("%d %d",&m,&n);
char **ar = new char*[m];
char *buf = new char[n];
int a,b;
for(a=0;a<m;a++)
{
gets(buf);
ar[a] = new char[n];
for(b=0;b<n;b++) ar[a][b] = buf[b];
}
for(a=0;a<m;a++,printf("\n")) for(b=0;b<n;b++) printf("%c",ar[a][b]);
return 0;
}
But it is behaving exactly the same.
Here is some sample input and output:
2 3
abc
def
output:
x��
abc
GDB doesn't seem to show anything up too. Please help..
It's not a "memory leak". The problem is that the first gets() call reads the newline from when you enter the two dimensions on the first line; it puts zero characters into the buffer, but you print 5, which is why you get a line of garbage.
Add a "\n" at the end of the scanf() format string so scanf() consumes the newline, and your program will work perfectly. Note that gets() is terribly unsafe; using fgets(buf, n, stdin) is much preferred.
In addition to missing '\n' in scanf() you should allocate more space for buf:
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n;
if(scanf("%d%d\n",&m,&n) != 2)
exit(EXIT_FAILURE);
char ar[m][n];
char buf[n+2]; // '\n\0'
int a,b;
for(a=0;a<m;a++)
{
if (!fgets(buf, n+2, stdin)) exit(EXIT_FAILURE);
for(b=0;b<n;b++) ar[a][b] = buf[b];
}
for(a=0;a<m;a++,printf("\n")) for(b=0;b<n;b++) printf("%c",ar[a][b]);
return 0;
}
Output
abc
def
input value 123 -- this value is integer, and valid
input value 1b23a -- this value is invalid
How do I detect which values are valid and not?
Here is my code:
#include <stdio.h>
#include <conio.h>
void main()
{
char str1[5],str2[5];
int num,num1,i;
num=0;
clrscr();
printf("Enter the Number ");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]>=48&&str1[i]<=56)
num=num1*10+(str[i]-48);
else
{
printf("The value is invalid ");
}
}
printf("This Number is %d",num);
getch();
}
Please see this answer regarding use of strtol(). It is a safe way to convert arbitrary input that should be a string representation of an integer, while also saving 'garbage' bytes for additional analysis.
Using it, your code would look something like this:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifdef LINUX_VERSION
#include <curses.h>
#else
#include <conio.h>
#endif
#define BUFF_SIZE 1024
int main(void)
{
char str1[BUFF_SIZE], *garbage = NULL;
long num = 0;
printf("Enter the Number ");
scanf("%s",str1);
errno = 0;
num = strtol(str1, &garbage, 0);
if (errno) {
printf("The number is invalid\n");
return 1;
}
printf("You entered the number %ld\n", num);
if (garbage != NULL) {
printf("Additional garbage that was ignored is '%s'\n", garbage);
}
getch();
return 0;
}
This doesn't fix everything that is questionable about what you posted, but it should help you get off to a better start.
Output is:
tpost#tpost-desktop:~$ ./t
Enter the Number 1234abdc
You entered the number 1234
Additional garbage that was ignored is 'abdc'
Compiled via:
gcc -Wall -DLINUX_VERSION -o t t.c -lcurses
I'm not sure what platform you are using, so additional fixes to the code may be needed.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[5],str2[5];
int num,num1,i;
num=0;
clrscr();
printf("Enter the Number ");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)
if(str1[i]>=48&&str1[i]<=56)
num=num1*10+(str[i]-48);
else
{
printf("The value is invalid ");
}
}
printf("This Number is %d",num);
getch();
}
One way is to use sscanf and check that there are no characters following the number. This is done most easily by adding a %c on the end and testing the return code, like this:
const char *yourString = ...;
int theValue, dummy;
if (sscanf(yourString, "%d%c", &theValue, &dummy) == 1) {
// Was a pure number, parsed into 'theValue'
} else {
// Either no number or had junk after it
}