reading a char at a time from text

This is a discussion on reading a char at a time from text within the C++ Programming forums, part of the General Programming Boards category; Hi, I'd like to read from a text file, but one char at a time. I can find plenty of ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    reading a char at a time from text

    Hi, I'd like to read from a text file, but one char at a time.

    I can find plenty of info on how to read in strings and manipulate them from a text file, but not much helpful stuff on doing that with single chars from a block of text.

    any suggestions?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    fgetc().
    Last edited by Dino; 01-29-2008 at 08:31 AM. Reason: oops = c++

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    char c;
    file >> c;
    File, of course, being an istream which you've opened your file with.

    Quote Originally Posted by Todd Burch View Post
    fgetc().
    Are we playing C now in the C++ section?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    wow, i guess it's simpler than i imagined.

    now bear with me on this next one:

    i need to specify exactly how many chars to read in, read them in, store them in an array, add the array to a queue, then do it again until i've reached the end of file.

    obviously i can write a function with a for loop to read in the chars (as many as i need at a time). But is there an automatic buffer that will hold my place in the file? (i.e. everytime the fucntion is called, will it automatically pick up where it left off in the file?)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    The file position in the file does not change so long as the file is open and you do not seek. So in essence, you can read a bit, do a lot of other work, then read a little again from the last position as long as you don't close the file. That means the object mustn't be destructed either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's normally how files and streams work - your "current position" will only move one character at a time when you read a character, and if you want to "jump", then you need to use "setpos" or some such function to actually change the current position.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The member function read() reads into a buffer.
    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

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Using an istreambuf_iterator<char> should also work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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