PDA

View Full Version : Need some help,,system calls



kodiak76
01-23-2005, 08:42 PM
Hello I am taking a class and I have to use system calls to make a program similiar to the cat utility in unix,,,I am using c++ on a linux server, I tried everything with no luck
I need to use the system calls open(); read(); and close();
I have a file called test.txt,, I have to use command line arguments to open a file for example my program is called mycat,,,I have to type on the command line

mycat test.txt when I hit enter in should open the file and display it on the screen... My code is below what am I doing wrong this code has errors and will not run
any advice will be helpful Thank you in advance


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(char *pathname, int oflag, int mode);
int read(int fd, char *buf, int size);
int close(int fd);

int main (int argc, char *argv[])
{
int fd;
int readd;
char buff[300];

fd = open(fd, O_RDONLY);
readd = read (fd, buff, 1);
close (fd);
return 0;
}

kermit
01-23-2005, 09:05 PM
First, you have #included <sys/types.h>, <sys/stat.h>, and <fcntl.h> already, but you forgot <unistd.h> (for the calls to read() and close()) Otherwise, you don't need to redo the function prototypes for open() read() or close() if you #include those four (above) header files. Try making those changes and you should at least be able to get an executable.

~/

kodiak76
01-23-2005, 09:27 PM
Thanks for the reply
i added #include <unistd.h> with no luck I get the following error message ,,,,,,

In function `int main(int, char **)':
mycat.cpp:16: no matching function for call to `open (int &, int, int)'
/usr/include/fcntl.h:66: candidates are: int open(const char *, int, ...)
mycat.cpp:6: int open(char *, int, int)

I am confused everything I have read this week end says that I have to code it like it is ,,,



#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int open(char *pathname, int oflag, int mode);
int read(int fd, char *buff, int size);
int close(int fd);

int main (int argc, char *argv[])
{
int fd;
int readd;
char *buff[300];

fd = open(fd, O_RDONLY, 0);
readd = read (fd, buff, 1);
close (fd);
return 0;
}

axon
01-23-2005, 11:01 PM
kodiak, please use [ code] [/ code] tags when posting code, if you don't know how, read the stickieds on the top of this forum.

Second of all, look how your open() function is declared, and then look what you're passing into it. Also man for some of these functions, maybe that will shed some light on what you're supposed to do.

quzah
01-24-2005, 01:02 AM
And since everyone seems to need everything handed to them on a silver plater around here:


int open(char *pathname, int oflag, int mode);

int fd;
int readd;
char *buff[300];

fd = open(fd, O_RDONLY, 0);There's your silver plater with colorful inlays.

Quzah.

kodiak76
01-25-2005, 12:30 AM
I made changes to my code and I get the following output
I have a file called test.txt on the linux command line I type
mycat test.txt It appears that my file is not opening or It is not reading the file.

2
mycat
test.txt
test.txt this test
3
9

Here is my code


#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>

using namespace std;
int main (int argc, char *argv[])
{
int fd;
int bread;
char buff [300];

cout << argc << endl;
cout << argv [0]<< endl;
cout << argv [1] << endl;

int i;
for (i = 1; i < argc; i++)
{
fd = open(argv[i], O_RDONLY);
cout << argv[i] << " this test" <<endl;
cout << fd<< endl;
}
bread = read (fd,buff, 9);
cout << bread << endl;
return 0;
}

jim mcnamara
01-25-2005, 12:25 PM
Usually you need to call read like this, especially on student systems which are frequently over-worked.




ssize_t readall(int fd, void *buf, size_t nbyte){
ssize_t nread = 0, n=0;

do {
if ((n = read(fd, &((char *)buf)[nread], nbyte - nread)) == -1) {
if (errno == EINTR)
continue;
else
return (-1);
}
if (n == 0)
return nread;
nread += n;
} while (nread < nbyte);
return nread;
}