Help saving contents of text file to a string

Hi, I'm having some trouble with an encryption program I'm writing. I'm trying to write a function that will read a text file of unknown length, capitalize every letter, and save it to a string. The contents of the text file would just be a several paragraph long message.

Here's what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void ProcessPlainTextFile(string& fileName, string& plainText)
{ 
    // a seperate function to open files
    OpenInputFile(fileName, textFile);
	 
	while ( getline(textFile, plainText))
	{
	   for ( unsigned int i = 0; i < plainText.length(); i++)
	   {
	       plainText[i] = toupper(plainText[i]);
	   }
	 cout << endl;
	}
	textFile.close();
}

The problem I am having is only the last line of the file gets saved to the string (plainText), which is the text to be encrypted. Any help would be appreciated!
- By the way, I'm using Visual Studios 2010.
That's because every time you call getline(), it overwrites the previous contents of the string with the new contents it is getting from the file. You could use a vector or something to store each line after capitalization.
Okay, ya now I see that it's just overwriting the string with every iteration of the loop... but I'm afraid I'm still pretty new to C++ so I'm not familiar with vectors yet. This program is for an assignment and I'm not allowed to use vectors or arrays for it, anyway. The focus of the assignment is just to get us familiar with functions though, and this particular function is just a very small part of the program, but I still can't figure out how to get the entire file saved to the string.
Got it! Just read the file char by char instead of line by line. Here's the code in case anyone who comes across this has a similar problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void ProcessPlainTextFile(string& fileName, string& plainText)
{ /* ProcessPlainTextFile */
	
    char line; 

    OpenInputFile(fileName, textFile);
	
	while (!textFile.eof())
	{
	textFile.get(line); 
	plainText += line; 
	     for ( unsigned int i = 0; i < plainText.length(); i++)
	     {
	          plainText[i] = toupper(plainText[i]);
	     }  
	}
	textFile.close();
} /* ProcessPlainTextFile */
Topic archived. No new replies allowed.