Thread: A File Pointer Question

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    A File Pointer Question

    I use fgets to grab a line to a string, and I checked the pointer before and after I use fgets. I want to know why the pointer remained the same, instead of add 200?

    Code:
    #include <stdio.h>
    
    int main (){
    
            FILE *new;
    	char string[100];
    	
    	new = fopen ("jt.rules", "r");
    	
    	printf ("start address: %d\n", new);
    	
    	fgets (string, 100, new);
    
    	printf ("end address: %d\n", new);
    	
    	fclose (new);
    	
        return 0;
    }

    output:

    start address: 1024748
    end address: 1024748

  2. #2
    Gentoo System .......... Doorsdown's Avatar
    Join Date
    May 2007
    Location
    Rhode Island, USA
    Posts
    19
    Found this while googling

    FILE objects are usually created by a call to either fopen or tmpfile, which both return a reference to one of these objects.
    The content of a FILE object is not meant to be read from outside the functions of the cstdio library; In fact, its main purpose is to be referenced as an argument in all stream-involving functions of this library to identify the stream to be affected.
    Its memory allocation is automatically performed by either fopen or tmpfile, and is the responsibility of the library to free the resources once the stream has been closed using fclose or other means.
    Last edited by Doorsdown; 10-15-2007 at 09:58 PM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by lukewang View Post
    I want to know why the pointer remained the same, instead of add 200?
    Because you have no idea what you're measuring.

    The FILE * isn't going to change. It's pointing to the location in memory where information is stored on the file. Where this information is stored isn't going to change. The information itself -- or to be more exact, parts of the information -- will change.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Quote Originally Posted by MacGyver View Post
    Because you have no idea what you're measuring.

    The FILE * isn't going to change. It's pointing to the location in memory where information is stored on the file. Where this information is stored isn't going to change. The information itself -- or to be more exact, parts of the information -- will change.
    Thanks for answering my question. But I'm still confused with how the pointer works. For example, if I use fgetc function for 5 times, how can I know where is my current position?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're not supposed to know. It's compiler and system specific. Check stdio.h and the other headers that your compiler may use and see if you can find the FILE struct definition. Remember, different compilers have different versions of it, so you should not make guarentees about this. Use the standard functions as much as possible.

    If you need more power, then you should look into operating system specific ways of handling files (which the C standard library functions actually use internally).

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Quote Originally Posted by lukewang View Post
    For example, if I use fgetc function for 5 times, how can I know where is my current position?
    You ask fgetpos or ftell. Which in turn asks the FILE * you give it (you can't ask it yourself, because as Mac said, you have no consistent way of knowing what it contains, but fgetpos does).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM