[B] Difference between open and fopen [/B]

This is a discussion on [B] Difference between open and fopen [/B] within the C Programming forums, part of the General Programming Boards category; i cant understand diff b/w open and fopen . please analysie below programs Code: #include<stdio.h> #include <sys/types.h> #include <sys/stat.h> #include ...

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    [B] Difference between open and fopen [/B]

    i cant understand diff b/w open and fopen .

    please analysie below programs


    Code:
    #include<stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main()
    {
    int f;
    char ch;
    f=open("/root/cprograms/file.txt",O_RDWR,0777);
    while(ch !='n')
    {
    ch=getc(f);
    printf("%c",ch);
    i++;
    }
    printf("\n");
    
    return 0;
    }

    Code:
    #include<stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main()
    {
    FILE *fd;
    char ch;
    fd=open("/root/cprograms/file.txt",O_RDWR,0777);
    while(ch !='n')
    {
    ch=getc(f);
    printf("%c",ch);
    i++;
    }
    printf("\n");
    
    return 0;
    }


    ouput i want to get as text in file.txt


    any body can help me?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    First of all your program just differs in return type of open

    But by your question in words i can say

    FOPEN
    fopen is available on any hosted ANSI C system and operates according to ANSI C. The existence and semantics of open depend on each system.

    On Unix, open and related function (such as lseek, read, write) each results in a transfer across protection boundaries into the kernel and do not use provide buffering in the user space of the process. fopen is more portable and generally more efficient. open allows additional, non-portable operations.

    OPEN
    Regarding OPEN is an POSIX system API. On systems that follow anything close to
    Unix or POSIX, open() accepts a flags field that contain flags that have functionality not available with fopen() or any other standard C library call.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,408
    Quote Originally Posted by nkrao123@gmail.
    i cant understand diff b/w open and fopen .
    open is non-standard (with respect to the C standard, but it is standard with respect to POSIX) but fopen is standard. Other than that just look up the relevant documentation, e.g., run man open and man fopen.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    fopen uses the newer, and easier to learn, stream file mode. open uses the older file handling method.

  5. #5
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Shall i print the charactes with open () ?

  6. #6
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Shall i print the charactes with open () ? from file.txt with while loop in my program?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,408
    Unless you have reasons to do otherwise, just use the C standard library I/O functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    What do you mean by priting the characters with open () dude ???

    And i think you should do man open and i think you will get all of your answers there and please read it with some patience.

  9. #9
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi now i learn more about open (it is a system call) .

    please let me know anyone, shall i print the characters with open() using while loop ?(please check the first program in post)

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No. Use fopen(), unless you explicitly require open.

  11. #11
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi ,

    i understand your suggestions . i want to write the program for displaying the characters using open() also using while loop from my first program in my post



    anybody can help me for open() to display characters from file.txt in my program?

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Check this out with a while loop

    Code:
    // An example of open                                                                                                                                                                                             
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    
    #define BUFFER_LENGTH 100
    
    int main() {
      int fd               = 0;
      char data_buffer[65] = {0};
      int length           = 0;
    
      fd = open("rocky.txt",O_RDONLY);
      if (fd != 0) {
        while (length = read(fd, data_buffer, BUFFER_LENGTH)) {
          data_buffer[length] = '\0';
          printf("******%s",data_buffer);
        }
      }
    }

  13. #13
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Rocky,

    ya .its working . thanks.

    from this replys i observed , main deference between open() and fopen() is

    open() is a systemcall

    fopen() is normal C library function


    is it right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen can't open .exe?
    By fanoliv in forum C Programming
    Replies: 11
    Last Post: 04-05-2011, 03:24 PM
  2. fopen and open
    By ygfperson in forum Linux Programming
    Replies: 2
    Last Post: 06-29-2005, 08:34 PM
  3. open, fopen ?
    By Matrim in forum C Programming
    Replies: 2
    Last Post: 02-01-2005, 08:45 AM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen vs open
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 12-10-2002, 01:30 PM

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