Thread: Files declaration confusion

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    38

    Files declaration confusion

    Hello. I'm confused about creating a file. I have found two possibilities:

    1) FILE *inputFilePtr;
    inputFilePtr = fopen("afile.txt", "rwb");

    Where the "r" in the second argument to fopen() opens the file for reading, and the "b" specifies binary mode.

    2) int file;
    char *filename;
    file = open(filename, O_WRONLY|O_CREAT);

    I have been using the second one without problems, but I really want to know difference between boths.
    I believe that both options create a binary file for reading and writing. Please correct if I'm wrong.

    Thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The second variety is a "lower level" file-management, and not part of the ANSI standard library. This in turn means that you have to change your code if you for example would like to move it to Windows from Linux.

    Technically, if you want to use open() for read & write, you should specify O_RDWR, and at this level, files don't have any concept of binary or not binary - you get the bytes that are in the file whatever they are.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to compile projects ?
    By manzoor in forum C++ Programming
    Replies: 31
    Last Post: 10-15-2008, 11:52 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM