0

std::string文字列を呼び出して出力ストリームに送信すると、ostream << string.c_str();正しく終了されません。どうしてこれなの?

class Application {
public:
    bool print() {
        out << "Content-Type: text/html; charset=utf-8\r\n\r\n";
        std::ifstream inFileStream;
        inFileStream.open("./test.html");
        if(!inFileStream.is_open()) {
            out << "Error Opening File";
            return true;
        }
        boost::uintmax_t templateSize = boost::filesystem::file_size("./test.html");
        std::string output;
        char* templateData = new char[templateSize];
        char* bytePtr = templateData;
        inFileStream.read(templateData, templateSize);

        std::ofstream logFile;
        logFile.open("/tmp/test.log");
        while(*bytePtr != EOF) {
            if(*bytePtr ==  '{')
                readVar(&bytePtr, &output);
            else
                output.push_back(*bytePtr);

            bytePtr++;
        }
        delete[] templateData;
        output.push_back(0);
        logFile << output.c_str();
        return true;

    }
private:
    void readVar(char** bytePtrPtr, std::string* output) {
        while(**bytePtrPtr != EOF) {
            if(**bytePtrPtr == '}')
                return;
            output->push_back('*');
            (*bytePtrPtr)++;
        }
    }
};

この出力(ログ ファイル内)には、適切に解析されたが含まれていますtest.htmlが、追加のバイト ガベージも含まれています。

4

1 に答える 1

2

読み取ったデータは で終了していませんEOF。ファイルの終わりと最初のファイルの間にあるジャンクをファイルからダンプしcharますEOF。への呼び出しの結果である文字outputを処理したら、文字を追加するループを停止する必要があります。nninFileStream.read(...)

于 2013-10-01T20:20:42.097 に答える