Thread: Tutorials about file I/O?

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Post Tutorials about file I/O?

    Hi people.

    Im making some progress in my quest to learn C, but Im having some problems finding good information about reading/writing files, well beyond just reading lines and typing them to the stdout anyways.

    Im looking at stuff like having configuration files, which requires a bit more.

    Im going to get myself a book or two, but until I do, I'd love to have some online information.

    Does anyone know any good sites with tutorials that dont just briefly touch the fgets, etc functions, but actually goes a little deeper?

    Oh and while Im asking, are there any particular books that are better than others?
    There are quite a few book on the main page, and Im having a hard time deciding which oens to get.
    Right now Im thinking I'll get "The C programming language", since I've read much of "The C++ programming language", and it was a good reference book.
    Aside from that Im also thiking a good tutorial style book would be good.

    Thanks.

    //Sunner

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    http://www.cprogramming.com/cboard/s...threadid=10358

    Haven't really looked, but seems to have some good stuff...

    Anyway, try google - bet it helps

  3. #3
    Sayeh
    Guest

    File I/O

    Reading/writing files is relatively straight forward. You use a function like either fread() or fwrite().

    Essentially any I/O storage device is a "block" device. This means that it isn't really engineered to read/write data in tiny (byte-size) quantities. You can, but that's misusing the power of the device itself.

    No, block devices prefer to be read/written in large "blocks" of data. 100K, 500K, or more. That's where they really shine.

    Understanding that means that rather than reading file data in in byte-size bits, you read in as big a block as is reasonable (100K, 50K, 10K, or larger like 1MB, or more depending on RAM).

    Same thing with writing.

    Since RAM is _so much faster_ than disk I/O, you want to do bulk reads and writes and then do your manipulation all in RAM. Makes for faster, more effecient programs.

    ---

    With the above basic understand, let's think about how we might use this:

    Well, instead of using fgets() to get a dribbling amount of data, you could ideally use fread() with a structure, or list of structures.

    A single diimension array of data is essentially a consecutive list of like elements. Because this is the case, you can actually read and write entire arrays as a single block of data from/to a drive.

    ---

    File basics--

    You can read data
    You can write data
    You can position the drive head within the file
    - Positioning is relative to the start of the file
    - Positioning can be relative to the end of the file
    - Positioning can be wherever the head is at


    ---

    Normally you open a file and get a FILE* pointer to it. This means the code creates a I/O buffer (at the driver level) and allocates a structure with a variety of other information about the file in question.

    You access this file by referencing the FILE*.

    In order to read from a file, you must create your own buffer (malloc() can do that) of sufficient size to contain the data you want to read in. To write to a file, you must already have a buffer allocated with the data you want written, and you must pass it's address to the fwrite() routine so the disk driver knows where to pull data from.

    ---

    What else would you like to know?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 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. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM