Thread: How would I get a certain character from a file, given the character number?

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question How would I get a certain character from a file, given the character number?

    I was wondering, how exactly do you go to a certain character number in a file? so suppose I was to open a file, and then wanted to obtain character number 26 from that file, how would I do it?


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You would use seekg() or perhaps seekp().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well ... to be fully cross-platform compatible, you would call get() 26 times. The seekg() effects are undefined for text mode streams.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    Quote Originally Posted by CornedBee View Post
    Well ... to be fully cross-platform compatible, you would call get() 26 times. The seekg() effects are undefined for text mode streams.
    How would one go about using the get function to do it? I assume that it needs to be called 26 times because there are 26 letters in the alphabet (english one anyway), so presumably, the function works by scanning the file for the letter 'a' and then scans again for 'b' etc, and prints all the a's and then all the b's, and so on.


  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    hmm... actually, in that case you may be able to use ignore(25) followed by a single get().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Finchie_88 View Post
    How would one go about using the get function to do it? I assume that it needs to be called 26 times because there are 26 letters in the alphabet (english one anyway), so presumably, the function works by scanning the file for the letter 'a' and then scans again for 'b' etc, and prints all the a's and then all the b's, and so on.
    Well, we're calling it 26 times because you said you wanted the 26th character from the file. get doesn't do any searching, it just gives you the next character, one at a time.

  7. #7
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    how would you go about doing this in C rather than using C++?


  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    With fgetc().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  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. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM