Thread: file handling in C

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    21

    file handling in C

    hii everyone..
    could anyone help me out with file handling in c.actually i wanted to know the command for reading the contents of a file from our computer.i mean we generally give a name to the file but if we want to retrieve the contents of a file from our system is it possible in c language?
    thanx and regards,
    cutelucks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Here's the FAQ article on dealing with files:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    File handling is definitely possible in C, although advanced file operations are system specific.

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You have to open the file using fopen(), then you can use any of the f* input functions to read from it. Search this forum for fopen() and you'll find lots of sample code.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    http://www.cppreference.com/
    that's a good list of some of the commonly used C and C++ libraries and the functions in them.

    http://www.cppreference.com/stdio/index.html
    From that same list, here's the IO functions for C, including file functions

    to open a binary file for reading,
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv) {
      FILE *f;
      char buf[BUFSIZ];
      int bs;
      f = fopen("file.bin","rb"); //use r for Read, w for Write, and b for Binary
      bs = fread(buf,1,BUFSIZ,f); //buffer, entry size (char = 1), entry count, file
      fclose(f);
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    21
    actually i want a text file content to be displayed in the command prompt..when i try giving the path like c:\a.txt..it says file not found..

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Backslash starts an escape sequence. Try using either "C:\\a.txt" or "C:/a.txt".

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    21
    thanx a lot..i got it..:d

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM