Thread: About file streams... Question.?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    About file streams... Question.?

    Hello ,
    I was wondering something. Assume we have a text file which contains : " Hello I am me". We use fgetc(fp) and we get H character. Next time we use fgetc(fp) we get e character. Very Nice. But I am wondering how the program can keep last character we took in its mind. I mean how can the program keeps the character we are on in its mind?

    Thank you for every response...

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't understand the question really. Are you trying to store the characters somewhere? You could store each char into a char array, but that's what fgets() is for.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    No not really , not that of a question. I'll try to explain again.
    You know each time we use fgetc with the same file pointer , we reach the next character from the text file. I wonder how can the program do that , how can it change the character each time , how can it recall where we stopped last time?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Oh, that's internal implementation. Without launching into great detail, the FILE struct usually contains a buffer of chars read from the underlying file from the O/S. It also contains a char * that points to the exact position in that buffer where you're currently at. When you call fgetc() it returns the char located at the pointer, and then increments the pointer by one.

    That is how I've seen it done, but those details are hidden from us and we can't assume anything about how they are done because they are done differently for each O/S and even up to a point for each compiler.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If you want to find out where you stopped, use ftell(), otherwise I have no idea what you're asking

    On some systems you can even map a file to memory, and move around it as if it was as so, just remember that it's sometimes a stream and doesn't necessarily end.
    Last edited by zacs7; 08-20-2007 at 06:30 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is probably more details than the original poster wanted to know, but essentialy somewhere low down in the filesystem, there is a counter/pointer to keep track of where in the file the last read ended (starting at zero when the file is opened for reading, normally). Reads, at this level are done in blocks (or multiples thereof). If a request from the user-level asks for less than a whole block, the read is "rounded up" to the block-size, and the whole block is kept in the buffer for that file - and when that is finished, the next block is read if there's more read requests.

    In the upper level, there is also various counters and buffers to hold some of the data coming from the file - this is because it's relatively expensive to call the OS to read small blocks of data - so the C-library will ask for a bigger block and deliver data from that block as/when necessary, until it's needs more data from the OS and does another read request.

    I hope this makes sense.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. a simple file stream question
    By terracota in forum C++ Programming
    Replies: 12
    Last Post: 07-11-2004, 12:03 AM