C++ Can';无法从文件中读取

C++ Can';无法从文件中读取,c++,mfc,fstream,C++,Mfc,Fstream,我正在尝试从.log文件中读取文本。我最初让它在windows控制台应用程序中工作,但现在我正在尝试使用MFC制作GUI版本。问题是,当我将while循环设置为while(file.good())时,它会完全跳过循环,我不知道为什么,因为file.open()没有错误 void转换::toXml(CString openPath,CString savePath) { CString null(“/0”); int c=openPath.GetLength(),q=0; 虽然(q问题可能是,当您

我正在尝试从.log文件中读取文本。我最初让它在windows控制台应用程序中工作,但现在我正在尝试使用MFC制作GUI版本。问题是,当我将while循环设置为while(file.good())时,它会完全跳过循环,我不知道为什么,因为file.open()没有错误

void转换::toXml(CString openPath,CString savePath)
{
CString null(“/0”);
int c=openPath.GetLength(),q=0;

虽然(q问题可能是,当您不需要反斜杠时,尝试将其转义,在路径中添加不必要的反斜杠。仅字符串文本需要转义反斜杠。可能是Windows不喜欢具有多个连续反斜杠的路径。

字符串
null
是包含三个字符的字符串:
>“/”
'0'
'\0'
。应该是这样吗?@JoachimPileborg:没关系:字符串从未被使用过。您的文件名构造产生了一个无效的文件名,因此打开所述文件名失败,ergo.file不是
。good()
。在调用open之前,请打印出文件名。看看它是否符合您的预期。看起来您对字符串和文件路径中的“\`字符都有一些误解。您在这里的想法是什么?如果您将示例简化为(编译并运行)再现您看到的结果的最小代码段,您将真正有所帮助(使用我们需要传入的输入)此代码是不可编译的(并且我们不知道您作为参数传入的文件名)。更简单的版本(稍微精简以删除不必要的
CStringA
->
const char*
无意义)对我来说效果很好。你观察到了什么症状?ifstream::is_open
返回true吗?@Nik我进行了转换,因为在我发布的第一段代码中,文件名是使用保存CString的CFileDialog检索的,所以我将其传递给函数。ifstream不会接受CString,而只接受一个const char*或至少一个是我遇到的错误。也是“打开”似乎起作用了吗?它被捕获在一个无限循环中,所以是吗?当添加一个手表和一些断点时,排序为。while循环启动1次执行getline(),然后退出循环并将“”分配给recText
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;

}