-3

I'm working on a code that reads in a C++ source file and converts all ‘<’ symbols to “<” and all ‘>’ symbols to “>”. I wrote out the main method and everything compiled nicely but now that I'm actually writing out my convert function at the top of the program, I'm stuck in an infinite loop and I'm hitting a wall on what the culprit is. Could someone help me out? I included the whole program in case the problem lies in my I/O coding but I surrounded the function with slashes. Hopefully I won't get flamed.

        #include <iostream>
        #include <fstream>
        #include <cstdlib>
        #include <string>
        #include <cstring>
        using namespace std;

//FUNCTION GOES THROUGH EACH CHARACTER OF FILE
//AND CONVERTS ALL < & > TO &lt; or &gt; RESPECTIVELY

//////////////THIS IS THE FUNCTION IN QUESTION//////////
void convert (ifstream& inStream, ofstream& outStream){
    cout << "start" << endl;
    char x;
    inStream.get(x);
    while (!inStream.eof()){
        if (x == '<')
            outStream << "&lt;";
        else if (x == '>')
            outStream << "&gt;";
        else
            outStream << x;
    }
    cout << "end" << endl;
};
///////////////////////////////////////////////////////////////////////////


int main(){

    //FILE OBJECTS
    ifstream inputStream;
    ofstream outputStream;
    string fileName;
    //string outFile;

    //USER PROMPT FOR NAME OF FILE
    cout << "Please enter the name of the file to be converted: " << endl;
    cin >> fileName;
    //outFile = fileName + ".html";

    //ASSOCIATES FILE OBJECTS WITH FILES
    inputStream.open(fileName.c_str());
    outputStream.open(fileName + ".html");

    //CREATES A CONVERTED OUTPUT WITH <PRE> AT START AND </PRE> AT END
    outputStream << " <PRE>" << endl;
    convert(inputStream, outputStream);
    outputStream << " </PRE>" << endl;

    inputStream.close();
    outputStream.close();

    cout << "Conversion complete." << endl;

    return 0;
}
user7472073
  • 11
  • 1
  • 2
  • 4
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Feb 21 '17 at 02:03
  • 2
    There are too many bugs here. Starting with the ol-reliable `while !feof` bug, and ending with a Loop Of Mystery, that magically expects some variable to change its value, for no reason whatsoever, somewhere in the loop... – Sam Varshavchik Feb 21 '17 at 02:04
  • 2
    Incredibly hard to reach the end of the file in a loop that never reads from the file. – user4581301 Feb 21 '17 at 02:05
  • 2
    Try calling get again in your loop. – Mikel F Feb 21 '17 at 02:05
  • 1
    Try doing e.g. `while (inStream >> x)` instead. – Some programmer dude Feb 21 '17 at 02:05
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Feb 21 '17 at 02:16

1 Answers1

0

It isn't a good approach to manipulate a file while you're reading it. The right way is, first read the whole file, store the data, manipulate the stored data, and then update the file. Hope this code will help you :)

   void convert()
    {
        int countLines = 0; // To count total lines in file
        string *lines; // To store all lines
        string temp;
        ifstream in; 
        ofstream out;
        // Opening file to count Lines
        in.open("filename.txt");
        while (!in.eof())
        {
            getline(in, temp);
            countLines++;
        }
        in.close();
        // Allocating Memory
        lines = new string[countLines];
        // Open it again to stroe data
        in.open("filename.txt");
        int i = 0;
        while (!in.eof())
        {
            getline(in, lines[i]);

            // To check if there is '<' symbol in the following line
            for (int j = 0; lines[i][j] != '\0'; j++)
            {
                // Checking the conditon
                if (lines[i][j] == '<')
                    lines[i][j] = '>';
            }
            i++;
        }
        in.close();
        // Now mainuplating the file
        out.open("filename.txt");
        for (int i = 0; i < countLines; i++)
        {
            out << lines[i];
            if (i < countLines - 1)
                out << endl;
        }
        out.close();
    }
Ahsan
  • 412
  • 5
  • 17