Changing characters in a File to all uppercase

I have been working to make all the characters in my file convert to all upper case. I have looked all over and asked people and I thought this should work but it's not reading my file. Any suggestions?

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int main()
{

cout << "Name: Greg Welsh \n"
"ID: 010550295 \n \n";

fstream Gplaintext, Gkey, Gencrypted, Gdecrypted;

Gplaintext.open("plaintext.rtf");

if (!Gplaintext)
{
cout << "Could not open file.";

return 0;
}


Gkey.open("Key.rtf");

if (!Gkey)
{
cout << "Could not open file.";

return 0;
}

for(int i = 0; i < strlen(Gplaintext); i++)
{
Gplaintext[i] = (toupper(Gplaintext[i])-'A')%25;
}

return 0;
}
In your for loop, strlen() will not work on a fstream, only on C-strings. You need to use some kind of loop that can check for end-of-file. Also to convert to uppercase you should check to make sure it is a letter(isalpha()) before using toupper() so when you do use it you can make it simple:

Gplaintext[i] = toupper(Gplaintext[i]);
this might be a stupid question but how can I make a loop that checks for the end-of-file?
while(!file.eof())
or
while(file.good())
One way of doing it:
1
2
3
4
5
6
7
8
9
bool file_toupper( const char* srce_file, const char* dest_file )
{
    std::ifstream in_file( srce_file ) ;
    std::ofstream out_file( dest_file ) ;
    in_file >> std::noskipws ;
    char c ;
    while( in_file >> c ) out_file << char( std::toupper(c) ) ;
    return in_file.eof() && out_file.good() ;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool uppercase(){
try{
inputfile.open(location here);
outputfile.open(location here);
}
catch(std::ifstream::failure &e){
std::cout << "an error has occured";
return false;
}

catch(std::ofstream::failre %o){
std::cout << "an error has occured";
return false;
}

char c; 
while(inputfile.good()){ inputfile >> c; outputfile << toupper(c);}
return true; 
Last edited on
Topic archived. No new replies allowed.