Thread: How to keep reading the same character in a file

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    How to keep reading the same character in a file

    Hi, I want to know how I can keep reading from the same position over and over. Currently my program uses fgetc(* FILE) but it by default will advance the pointer to the next position. Is there another fFunction that helps you choose the position of the pointer in the text file. Thanks.
    -V

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fseek.
    Why would one want to read from the same position again and again?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nndhawan View Post
    Hi, I want to know how I can keep reading from the same position over and over. Currently my program uses fgetc(* FILE) but it by default will advance the pointer to the next position. Is there another fFunction that helps you choose the position of the pointer in the text file. Thanks.
    -V
    Bayint Naung beat me to the question...

    But does it not strike you as a good idea to take that one bit of file, store it in a variable (in memory) and use the variable rather than pounding your hard disk to death?

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    What does it matter why? How can I do it?

    Right now I have to keep opening and closing my File stream to reset the pointer which sucks and is way worse then just being able to decrement the pointer one time. Any good ideas?

    Is there other operations that give you the actual memory location of the file? Either way the reason why is cause I'm reading a gpio port found in /sys/class/gpio/ and want to be able to constantly check what value it is.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nndhawan View Post
    What does it matter why? How can I do it?
    Gee I dunno... maybe I cared enough to not want you to get stuck replacing a disk drive....
    Silly me!

    Right now I have to keep opening and closing my File stream to reset the pointer which sucks and is way worse then just being able to decrement the pointer one time. Any good ideas?

    Is there other operations that give you the actual memory location of the file? Either way the reason why is cause I'm reading a gpio port found in /sys/class/gpio/ and want to be able to constantly check what value it is.
    First is the stream seekable?

    If so look into using fread(string,blocksize,1,file) and fseek(file, -blocksize, SEEK_CUR).

    Most non seekable streams will simply deliver the next available block with fread(), fgets() etc. with no need to close open etc between reads.
    Last edited by CommonTater; 04-04-2011 at 05:45 PM.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nndhawan View Post
    What does it matter why? How can I do it?
    We ask why, because people often come in here and ask, "Where can I get a gun?" We tell them where to get a gun -- they point it at their foot and pull the trigger

    In other words, we want to know more about the problem, not your (assumed) solution to the problem. It's more helpful to you in the long run, and it saves us from feeling guilty about guiding you off the edge of a cliff.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM