I / O程序卡在循环C ++中

时间:2017-02-21 02:01:53

标签: c++ file io

我正在编写一个读取C ++源文件的代码,并将所有'<'符号转换为“<”和所有'>'符号转换为“>”。我写出了主要的方法和所有编译得很好但现在我实际上在程序的顶部写出了我的转换函数,我陷入了无限循环而且我正在追究罪魁祸首的问题。有人可以帮帮我吗? 我把整个程序包括在内,以防问题出在我的I / O编码中,但我用斜线包围了函数。希望我不会被焚烧。

        #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;
}

1 个答案:

答案 0 :(得分:0)

在阅读文件时,操作文件并不是一个好方法。正确的方法是,首先read整个文件,store数据,manipulate存储的数据,然后update文件。希望这段代码可以帮到你:)

   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();
    }