0

.log ファイルからテキストを読み取ろうとしています。私はもともと Windows コンソール アプリケーションで動作していましたが、現在 MFC を使用して GUI バージョンを作成しようとしています。問題は、while ループを while(file.good()) として設定すると、ループが完全にスキップされ、file.open() にエラーがないため理由がわかりません。

void Conversion::toXml(CString openPath, CString savePath)
{
CString null("/0");

int c = openPath.GetLength(),q=0;
while(q<c)
{
    if(openPath[q] == '\\')
    {
        openPath.Insert(q,'\\');
        q++;
    }
    q++;
}

CStringA charstr(openPath);
const char* oPath((const char *) charstr);
CStringA charstr2(savePath);
const char* sPath((const char *) charstr2);

ifstream openFile;
ofstream savedFile(sPath);

string recText = "";
string temp;
openFile.open(oPath);

while(openFile.good())
{
    getline(openFile,temp);

    recText = recText + temp;
}

小さいバージョン (コンパイルする必要があります)

#include <iostream>
#include <fstream>
#include <string>
#include "stdio.h"
#include <windows.h>
#include <algorithm>
#include <atlstr.h> 


using namespace std;

int main()
{
string recText = "";
string temp;
CString path = "C:\\Users\\name\\Desktop\\2012-08-281.log";
CStringA charstr(path);
const char* oPath((const char *) charstr);


ifstream xmlDoc (oPath);
ofstream finished ("C:\\Users\\name\\Documents\\example3.xml");


while(xmlDoc.good())
{
    getline(xmlDoc,temp);

    recText = recText + temp;
}
    finished<<recText<<endl;
    return 0;

}
4

1 に答える 1

0

問題は、パスに不要なバックスラッシュを追加して、必要のないバックスラッシュをエスケープしようとすることです。バックスラッシュのエスケープは、文字列リテラルにのみ必要です。Windows は、複数のバックスラッシュが連続するパスを好まない可能性があります。

于 2013-03-01T15:30:23.197 に答える