Preserving spaces in the following loop

I am reading the characters in one by one from a .txt file in order to manipulate them individually. I've found a way to preserve the punctuation/numbers, but I don't know how to preserve the whitespaces.

I know that >> ignores the white spaces by default. What can I do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while (!fromFile.eof()){
		 fromFile >> ch;
		if(islower(ch)){
			ch -=shift;
			if(ch < 'a'){
				ch+=26;
			}
		}
		if(isupper(ch)){
			ch -=shift;
			if(ch < 'A'){
				ch +=26;
			}
		}

	cout << static_cast<char>(ch);
	
	}


Last edited on
try get() command. The following link gives a nice explanation with a relevant example:

http://www.cplusplus.com/reference/istream/istream/get/
I can't find any mention of is.good anywhere. What is that function?

while (is.good()) // loop while extraction from file is possible
good is a member function. is is a variable. What is the type of is?
I guess acemanhattan is referring to the following example code on this site:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// istream::get example
#include <iostream>     // std::cin, std::cout
#include <fstream>      // std::ifstream

int main () {
  char str[256];

  std::cout << "Enter the name of an existing text file: ";
  std::cin.get (str,256);    // get c-string

  std::ifstream is(str);     // open file ("is" is an instance of type ifstream)

  while (is.good())          // "good()" is a Boolean member function. you can checkout the reference for std::ios::good() on this website.
  {
    char c = is.get();       // get character from file
    if (is.good())
      std::cout << c;
  }

  is.close();                // close file

  return 0;
}
The reference section of this site is very useful.
Last edited on
Topic archived. No new replies allowed.