Strings Reading from a txt file

0 John Shaw · October 21, 2014
Hi all,

I have been trying something with reading txt files,
What I'm trying to do is :- Read the txt file in char by char, and out-put it to the screen.

#include <iostream>  // Pre-processor directive
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
int main()  // Title of our function (main)
    string str;
    char crt;
    ifstream theFile("Test.txt");
 while(theFile>>str)   // // While this condition is TRUE the Loop continues
cout<<str<<" ";
Sleep(25); // This is to simulate some one typing the txt as it's displayed



If I use the str variable, the file is read in and output to the screen with a space between each word as you would expect (No problem there!! all good!)

If however change the str to  crt (including the while command of course), the file is still read in, and out-put to the screen, But there are no Blank Spaces, to denote the end of each word.

I have fixed the problem  !!!
Using 'noskipws'


Post a Reply


Oldest  Newest  Rating
0 ragmar thomas · October 22, 2014
when using string to input the whole line with the spaces use
getline(cin, str)
getline(theFile, str);//for your case

if you use char you can

cin.getline(crt, size of the array)
char crt[20];//something like this or more array size
0 John Shaw · October 22, 2014
Thanks, that provides me with another method to use.

Really appreciate the help.
Take care :)
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator