954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trying to get a while loop while using an input/output file

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;
}
kotkata
Newbie Poster
15 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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;

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
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.

kotkata
Newbie Poster
15 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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?

kotkata
Newbie Poster
15 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

So we can mark thisone as solved?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You