简体   繁体   English

无法读取文件

[英]Can't read from file

I'm trying to read text from a .log file. 我正在尝试从.log文件读取文本。 I originally have it working aa windows console application but I'm now trying to make a GUI version using MFC. 我本来可以在Windows控制台应用程序中使用它,但是现在我正在尝试使用MFC创建GUI版本。 The problem is when i set the while loop as while(file.good()), it completely skips the loop and I'm not sure why because there are no errors with file.open() 问题是当我将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;
}

smaller version(should compile) 较小的版本(应编译)

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

}

The problem might be that you try to escape the backslash when you don't need it, adding unnecessary backslashes in the path. 问题可能是您在不需要时尝试转义反斜杠,从而在路径中添加了不必要的反斜杠。 Escaping backslashes is only needed for string literals . 字符串文字只需要转义反斜杠。 It might be that Windows doesn't like paths with multiple consecutive backslashes. Windows可能不喜欢带有多个连续反斜杠的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM