Need some help,,system calls

This is a discussion on Need some help,,system calls within the Linux Programming forums, part of the Platform Specific Boards category; Hello I am taking a class and I have to use system calls to make a program similiar to the ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Need some help,,system calls

    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;
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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.

    ~/

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    3
    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;
    }

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And since everyone seems to need everything handed to them on a silver plater around here:
    Code:
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    3
    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

    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;
    }

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    Usually you need to call read like this, especially on student systems which are frequently over-worked.
    Code:
    
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. syslog calls and threading
    By rotis23 in forum Linux Programming
    Replies: 2
    Last Post: 06-28-2005, 10:49 AM
  4. Which I/O calls do you use the most?
    By _Elixia_ in forum Tech Board
    Replies: 10
    Last Post: 07-11-2003, 12:58 PM
  5. C/C++ Memory Calls!
    By Joda in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2002, 05:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21