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

This is a discussion on How would I get a certain character from a file, given the character number? within the C++ Programming forums, part of the General Programming Boards category; I was wondering, how exactly do you go to a certain character number in a file? so suppose I was ...

  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
    22,170
    You would use seekg() or perhaps seekp().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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
    22,170
    hmm... actually, in that case you may be able to use ignore(25) followed by a single get().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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,893
    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, 06:59 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21