Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to read a character at a time from file??

How to read a character at a time from file??

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpdata-structureshelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ConradT
    wrote on last edited by
    #1

    Hi all, I'm new to C++ and am trying to write a kind of predictive text program where the text from a file will attempt to be predicted by the program. Atm I'm trying to get started by reading the text from the file a character at a time and having each character be displayed to the screen after a press of a key. I've managed to read the text and display it as a complete word but am having trouble reading and displaying it a character at a time. I've messed around with cin.get, cin.getc, cin.getchar etc and can't seem to get them to work correctly :( Any help on how to go about doing this would be greatly appreciated, Thanks. The short program below is how I read the text from the file and displayed it to the screen: Note, to avoid the console window disappearing, you have to run it using CTRL+F5. If there is another way to stop it closing please let me know. I'm using MS Visual C++.NET btw. ===================== #include #include #include using namespace std; int main() { char buffer[256]; // Create 256 bit character array. ifstream file_op; // Create an output stream variable. file_op.open("mytext.txt"); // Open the text file to read from. while(file_op >> buffer) cout << buffer << endl; // Display complete text (complete word) file_op.close(); return 0; }

    K D 2 Replies Last reply
    0
    • C ConradT

      Hi all, I'm new to C++ and am trying to write a kind of predictive text program where the text from a file will attempt to be predicted by the program. Atm I'm trying to get started by reading the text from the file a character at a time and having each character be displayed to the screen after a press of a key. I've managed to read the text and display it as a complete word but am having trouble reading and displaying it a character at a time. I've messed around with cin.get, cin.getc, cin.getchar etc and can't seem to get them to work correctly :( Any help on how to go about doing this would be greatly appreciated, Thanks. The short program below is how I read the text from the file and displayed it to the screen: Note, to avoid the console window disappearing, you have to run it using CTRL+F5. If there is another way to stop it closing please let me know. I'm using MS Visual C++.NET btw. ===================== #include #include #include using namespace std; int main() { char buffer[256]; // Create 256 bit character array. ifstream file_op; // Create an output stream variable. file_op.open("mytext.txt"); // Open the text file to read from. while(file_op >> buffer) cout << buffer << endl; // Display complete text (complete word) file_op.close(); return 0; }

      K Offline
      K Offline
      KRowe
      wrote on last edited by
      #2

      look in the help for getch(); you will need to include stdio.h.

      1 Reply Last reply
      0
      • C ConradT

        Hi all, I'm new to C++ and am trying to write a kind of predictive text program where the text from a file will attempt to be predicted by the program. Atm I'm trying to get started by reading the text from the file a character at a time and having each character be displayed to the screen after a press of a key. I've managed to read the text and display it as a complete word but am having trouble reading and displaying it a character at a time. I've messed around with cin.get, cin.getc, cin.getchar etc and can't seem to get them to work correctly :( Any help on how to go about doing this would be greatly appreciated, Thanks. The short program below is how I read the text from the file and displayed it to the screen: Note, to avoid the console window disappearing, you have to run it using CTRL+F5. If there is another way to stop it closing please let me know. I'm using MS Visual C++.NET btw. ===================== #include #include #include using namespace std; int main() { char buffer[256]; // Create 256 bit character array. ifstream file_op; // Create an output stream variable. file_op.open("mytext.txt"); // Open the text file to read from. while(file_op >> buffer) cout << buffer << endl; // Display complete text (complete word) file_op.close(); return 0; }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Why not change buffer to be a single character?


        "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups