Sending 3 arguments from one program to another using execvp? Error?

Hi everyone,

I'm creating an ALU and CPU
ALU's job is to receive 3 arguments from CPU and compute them and return the result of computation

CPU's Job is to read a file called sample.txt and SEND THOSE ARGUMENTS TO ALU.cpp

This is my work so far

ALU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <stdlib.h>
using namespace std;

int Operation(int a,char* op,int b) {

char operand = *op;
if(operand == '+') {
return a+b;
}
if(operand == '-') {
return a-b;
}
if(operand == '/'){
return a/b;
}
if(operand == '*'){
return a*b;
}
else {
cout << "invalid" << endl;
return -999;
}
}
int main (int argc,char* argv[]) {
if(argc!=4) {
cout << "Exit" << endl;
return -9999;
}
else {
for(int i=1;i<argc;i++) {
cout << "Arg " << i << ": " << argv[i] << endl;
}
int arg1,arg2;
char* op;
arg1= atoi(argv[1]);
arg2= atoi(argv[3]);
op = argv[2];
int result = Operation(arg1,op,arg2);
cout << "result: " << result << endl;
return result;
}
return 0;
}



CPU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;


int main () {
ifstream obj;
obj.open("sample.txt",ios::app);
string arg1,arg2;
string op;
pid_t pid = fork();

while(
getline(obj,arg1,',') && getline(obj,op,',') && getline(obj,arg2,'\n')
) {
cout << arg1 << op << arg2 << endl; //Just for testing
}
if(pid==0) {
string args[] = {arg1,op,arg2};
execvp("ALU.cpp",args);  //error
}
}


I'm receiving errors in CPU.cpp and I tried resolving it many times am I even doing this right? I'm really confused after debugging for 4 days straight

I really need some guidance on this one
Last edited on
I'm receiving errors in CPU.cpp

The first guidance I'd give you is don't keep secrets, and don't expect us to be able to read your mind. If you have errors, then tell us what they are. You're the one looking for people to give their time and effort to help you, so the least you could do is take the time and effort to give us as complete, and helpful, a description of the errors your getting as possible, so that we don't have to do all the work of figuring out what it is you want.

Looking at your CPU.cpp, I can see two obvious problems:

1) You're using an ifstream to read from a file, but you're trying to use ios::app with it. Why? What dense does it make to open a file in that mode, when you're only reading from it?

2) You're trying to run ALU.cpp as if it were an executable file, when it's not - it's a source file. Do you understand that you need to compile source code to create an executable, and that you run the executable?
It would help if you indented your code.

Do you need all those header files?

op should be of type char, not char*. And you'd initialize it with: char op = *argv[2];, or something like that.
@MikeyBoy
You're right I'm sorry about that but that's the problem I can't understand what the error is
1)I opened the file in append mode so the file content doesnt get deleted automatically (it happens)

2)I'm working on linux I'm not sure how I create one but now that you mention it I'll give it a go

Thank you :)
Last edited on
@kbw yes they are required
and I'll give it a go
You're right I'm sorry about that but that's the problem I can't understand what the error is

Tell us what you're seeing that makes you think you have errors Is your compiler giving you error messages? Is your program crashing? Are you seeing some other behaviour that differs from what you expect?

The more information you give us about your problem, the easier it is for us to help you.

Surely this should be blindingly obvious?

I opened the file in append mode so the file content doesnt get deleted automatically (it happens)

It doesn't happen if you open the file for reading, not writing. Which, obviously, is what you should be doing here.

I'm working on linux I'm not sure how I create one but now that you mention it I'll give it a go

This makes no sense at all. You can't possibly have been working with C++ all this time without compiling code to executables. You wouldn't have ever been able to run anything you've ever written, ever, without doing so.
exec... take the exe name as the first arg, but also the first arg in the arg list. That's why it's not working.

You're not even passing the executable name, you're passing the source file name.

You don't need all those header files. I suggest you remove them all, then add ones you need.
Last edited on
Topic archived. No new replies allowed.