Basically this code serves to take in an input file and convert all the "<" and ">" into "&lt" and "&gt" respectively. This works however, I cannot get it to convert the rest of the .txt file into the output file. Right now, it just works with the very first character in the text file.
How can I make this repeat until the end of the .txt file so that all characters in it are either converted or left alone?

I appreciate any and all help.

#include<fstream>
#include<iostream>
#include<cstdlib>
#include<stdlib.h> 
using namespace std; 

void checker (char next);
string ltreplace (char next);
string gtreplace (char next);

int main ()
{
     ifstream inStream;
     ofstream outStream;
     string infileName, outfileName;
     cout << "What file would you like to work with?" << endl; 
     cout << "Remember to include the file extension." << endl;
     cin >> infileName;
     cout << "What would you like to call the output?" << endl;
     cin >> outfileName;
     inStream.open(infileName.c_str()); //to connect with file
     if (inStream.fail())
     {
        cout << "File open failed.\n";
        getchar();
        getchar();
        exit(1);
     }
     
     char next;
     string replace;
     inStream >> next;
     while (!inStream.eof()) //while loop not working
     {
           if (next == '<') 
            { 
              outStream.open(outfileName.c_str(), ios::app); //appends to the end of the file each time
              outStream << ltreplace(next);
            }
           else if (next == '>')
            {
              outStream.open(outfileName.c_str(), ios::app); //appends to the end of the file each time
              outStream << gtreplace(next);
            }
           else 
           {
               outStream.open(outfileName.c_str(), ios::app); //appends to the end of the file each time
               outStream << next;
           }
           inStream >> next;
     }
     return 0;
}

string ltreplace (char next) 
{
     string replacement;
     replacement = "&lt";
     return replacement;
}

string gtreplace (char next) 
{
     string replacement;
     replacement = "&gt";
     return replacement;
}

Recommended Answers

All 6 Replies

Replace this:

inStream >> next;
while (!inStream.eof()) //while loop not working

with this:

while (inStream >> next) //while loop working
{

and do this:

outStream.open(outfileName.c_str(), ios::app); //appends to the end of the file each time

only once, before the while loop. And drop the inStream >> next in the following:

inStream >> next;
}
return 0;

char next;
     string replace;
     inStream >> next;
     outStream.open(outfileName.c_str(), ios::app);
     while (inStream >> next) //while loop not working
     {
           if (next == '<') 
            { 
              outStream.open(outfileName.c_str(), ios::app); //appends to the end of the file each time
              outStream << ltreplace(next);
            }
           else if (next == '>')
            {
              outStream.open(outfileName.c_str(), ios::app); //appends to the end of the file each time
              outStream << gtreplace(next);
            }
           else 
           {
               outStream.open(outfileName.c_str(), ios::app); //appends to the end of the file each time
               outStream << next;
           }
     }
     return 0;
}

Is this what you had in mind? I tried to do but now it doesnt return anything. I probably missed something in your explanation.

GORT!
Why is the outStream opened every time?
I may be wrong but I believe once is enough.

Ok ddanbe, thank you that solved the problem of it only writing the first character.

Now a new problem arises, is there any way to make it recognize whitespaces as characters?

Don't use the >> operator. Use one of the get() or getline() functions instead.

So we can mark thisone as solved?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.