Need guide using open()

This is a discussion on Need guide using open() within the C Programming forums, part of the General Programming Boards category; Hi, I have a utility that uses fopen(), fseek(), getc(). I want to use open() instead of fopen(). fopen() => ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    Need guide using open()

    Hi,
    I have a utility that uses fopen(), fseek(), getc(). I want to use open() instead of fopen().
    fopen() => open()
    What about the other functions? is there any equivalent functions for them?

    I searched google for examples, tutorials for using functions of <fcntl.h> library, but the results were not enough and very little

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Note that the lower-level I/O functions are platform-specific, but generally:

    fopen -> open
    fseek -> lseek
    getc -> read(fd, buf, 1)

    There's no equivalent low-level I/O function for getc()...you'll have to read() 1 byte into a buffer and then grab the first character of the buffer.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    getc -> read(fd, buf, 1)
    Why (1)

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Because getc() returns 1 character. You can read() as many as you want into buf, but if you only want 1 character then you only need to read() 1 byte.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
    itsme@itsme:~/C$ cat readexample.c
    #include <stdio.h>
    #include <unistd.h>
    
    int main(void)
    {
      char c, buf[1];
    
      printf("Type a character: ");
      fflush(stdout);
      c = getc(stdin);
    
      printf("Type another character: ");
      fflush(stdout);
      read(STDIN_FILENO, buf, 1);
    
      printf("Using getc() you typed: %c\n", c);
      printf("Using read() you typed: %c\n", buf[0]);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./readexample
    Type a character: i
    Type another character: t
    Using getc() you typed: i
    Using read() you typed: t
    itsme@itsme:~/C$
    itsme@itsme:~/C$
    It prints the prompt twice at the end because I didn't read() the '\n' from pressing enter after typing the second character.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Code:
    #include <stdio.h>
    #include <fcntl.h>
    
    int main()
    {
    
        int fp;
        char buf[1];
        int e;
        e = 10;
        fp = open("file1", O_RDONLY);
    
        if(fp==-1){
            printf("Could not open file \n");
            exit(1);}
        lseek(fp,e,SEEK_SET);
        while(read(fp,buf,1)!=0){
            printf("%c",buf[0]);
            }
    
        close(fp);
        return 0;
    }
    This code is just to learn how to use (fcntl.h) functions, I wanted to read the bytes that are located after 10 bytes. But, it seems that this way does not afftect at all. The out put of this code is a like if I used 'lseek' or did not.
    Last edited by Moony; 07-10-2006 at 03:18 AM.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You should check the return value of lseek() to see if it failed.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Yes, it failed. I tried that.
    Why?

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    I dunno...#include <string.h> and <errno.h> and then when lseek() fails do: puts(strerror(errno));
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I did that and recived:
    Invalid argument

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Not sure why yours is failing, but you're not including all of the necessary header files:
    Code:
    itsme@itsme:~/C$ ./lseektest
     least 10 characters
    itsme@itsme:~/C$ cat lseektest.c
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <errno.h>
    
    int main()
    {
    
        int fp;
        char buf[1];
        int e;
        e = 10;
        fp = open("file1", O_RDONLY);
    
        if(fp==-1){
            printf("Could not open file \n");
            exit(1);}
        if(lseek(fp,e,SEEK_SET) == -1)
        {
          puts(strerror(errno));
          exit(1);
        }
        while(read(fp,buf,1)!=0){
            printf("%c",buf[0]);
            }
    
        close(fp);
        return 0;
    }
    Code:
    itsme@itsme:~/C$ gcc -Wall lseektest.c -o lseektest
    lseektest.c: In function `main':
    lseektest.c:17: warning: implicit declaration of function `exit'
    lseektest.c:18: warning: implicit declaration of function `lseek'
    lseektest.c:23: warning: implicit declaration of function `read'
    lseektest.c:27: warning: implicit declaration of function `close'
    itsme@itsme:~/C$ echo "this is at least 10 characters" > file1
    Code:
    itsme@itsme:~/C$ ./lseektest
     least 10 characters
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Invalid argument. This is used to indicate various kinds of problems with passing the wrong argument to a library function.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    First I would try including all the necessary header files. They're required for a reason.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    I included all necessary header files, even I copied the code that you posted. Still recieve that message.

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    My code didn't have all the necessary headers. Here...include all of these:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <unistd.h>
    #include <sys/types.h>
    Turn on your compiler warnings...
    If you understand what you're doing, you're not learning anything.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open Source Licenses
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-10-2006, 08:53 PM
  2. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Problems with open and save boxes
    By pinkcheese in forum Windows Programming
    Replies: 3
    Last Post: 05-21-2002, 06:03 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

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