无法从文件中读取
Can't read from file
我正试图从.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;
}
问题可能是您试图在不需要反斜杠时转义它,从而在路径中添加了不必要的反斜杠。转义反斜杠只需要用于字符串字面值。可能是Windows不喜欢有多个连续反斜杠的路径
相关文章:
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 读取文件的最后一行并输入到链接列表时出错
- 为什么在读取文件大小时文件IO速度会发生变化
- 读取文件时运行时的未知行为
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- C++ 读取文件读取文件不正确
- 读取文件并将其存储在unordered_map中时出现问题
- 读取文件时无法使用 OpenMP 获得加速
- 使用istringstearm和get行缓慢读取文件
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 读取文件时引发异常
- 从标准输入读取文件后读取用户输入
- 在读取文件后重置句柄
- 如何在C++编译时读取文件?
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- 读取文件在第二次调用时返回INVALID_HANDLE
- 通过指针读取文件
- 逐行读取文件,并将数据插入变量和数组中