File Handling in C - File Pointers

Discussion in 'C' started by pradeep, Sep 2, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Refer to the recent articles on Understanding File Handling Functions in C & Understanding Advance File Handling Functions in C

    C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and written as FILE *. A file pointer called output_file is declared in a statement like

    Code:
    FILE *output_file;

    Opening a file pointer using fopen



    Your program must open a file before it can access it. This is done using the fopen function, which returns the required file pointer. If the file cannot be opened for any reason then the value NULL will be returned. You will usually use fopen as follows

    Code:
     if ((output_file = fopen("output_file", "w")) == NULL)
            fprintf(stderr, "Cannot open %s\n", "output_file");
    fopen takes two arguments, both are strings, the first is the name of the file to be opened, the second is an access character, which is usually one of:

    "r" => Open a file for reading
    "w" => Create a file for writing
    "a" => Open a file for appending

    Closing a file using fclose



    The fclose command can be used to disconnect a file pointer from a file. This is usually done so that the pointer can be used to access a different file. Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.

    This would be done using a statement like

    Code:
    fclose(output_file);
    If files are still open when a program exits, the system will close them for you. However it is usually better to close the files properly.

    Standard file pointers in UNIX



    UNIX systems provide three file descriptors which are automatically open to all C programs. These are

    stdin => The standard input.The keyboard or a redirected input file.
    stdout => The standard output.The screen or a redirected output file.
    stderr => The standard error.This is the screen, even when output is redirected.
    This is a conventional place to put any error messages.

    Since these files are already open, there is no need to use fopen on them.
     
    shabbir likes this.
  2. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
    thanks a lot , file handling is realyy tough forme
     
  3. sreeramu

    sreeramu New Member

    Joined:
    Oct 18, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    if you explain other file related functions in ths thread means it will be very nice....
     
  4. livinmanavalan

    livinmanavalan Banned

    Joined:
    Dec 6, 2010
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I am also needed details for it.
     
  5. rameshb

    rameshb New Member

    Joined:
    Dec 10, 2010
    Messages:
    35
    Likes Received:
    1
    Trophy Points:
    0
    Please provide us with a example program so that the concept file handling is more clear to us
     
  6. alvisnally

    alvisnally New Member

    Joined:
    May 18, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Well, A file is a collection of bytes stored on a secondary storage device, which is about a basic of some kind. The accumulating of bytes may be interpreted. There are two kinds of files that programmers deal with text files and binary files.
     
  7. jennyjackson

    jennyjackson New Member

    Joined:
    May 17, 2011
    Messages:
    14
    Likes Received:
    1
    Trophy Points:
    0
    I also want information about it.
     
  8. mukund077

    mukund077 New Member

    Joined:
    Aug 12, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    My file is in binary format. The data is actually encrypted in binary form. the data format is double. How can i actually get my data extracted form this encrypted data using C programming. Can I print the value of the pointer's location(please not its not the value at the pointer location ).??
     
  9. satya.139

    satya.139 New Member

    Joined:
    Aug 13, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    is there any way to delete a record from a file without copying to some other temporary file in c
     
  10. john1110

    john1110 New Member

    Joined:
    Jul 25, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    thanks to share this pointer info
     
  11. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Great Tutorial, but you could also add a binary opening.
     
  12. etsuser

    etsuser Banned

    Joined:
    Oct 1, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I am also needed details for it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice