Reputation: 151
I am new in C program and linux, how can we compile and run this program?
I have tried gcc example.c
then ./a.out
but it gives an error like input file cannot be opened
( I have written this error in the read method)
// example.c
int main(int argc, char *argv[])
{
char* input = argv[1];
read(input);
char* output = argv[2];
write(output);
return 0;
}
Thanks.
Upvotes: 2
Views: 21432
Reputation: 25705
Firstly read()
and write()
both take 3 arguments(only one given).
Secondly it should be used like this:
int ifilehandle = open(argc[1],O_RDONLY,S_IREAD);
int ofilehandle = open(argc[2],O_WRONLY,S_IWRITE);
char buffer[32767];
read(ifilehandle,&buffer,32766);
write(ofilehandle,&buffer,32766);
close(ifilehandle);
close(ofilehandle);
Thirdly, a.out should be invoked like this:
./a.out filename1.extension filename2.extension
Upvotes: 1
Reputation: 224962
Your program isn't going to work very well - you're not providing enough arguments to read
and write
, for example (assuming you mean to be calling POSIX read(2)
and write(2)
, that is).
To answer your actual question, the problem appears to be that you're not providing any arguments. You need to run it something like:
./a.out FILE1 FILE2
replacing FILE1
with the name of your input file and FILE2
with the name of your output file.
Upvotes: 3