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 回答