How to read a character at a time from file??
-
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; }
-
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; }
-
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; }
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