2013-03-01 6 views
0

.log 파일에서 텍스트를 읽으려고합니다. 원래는 Windows 콘솔 응용 프로그램을 사용하고 있지만 이제는 MFC를 사용하여 GUI 버전을 만들려고합니다. 내가 동안으로 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; 

} 
+0

'null'문자열은 '/', ''0 '및' '\ 0' '의 세 문자를 포함하는 문자열입니다. 그게 그렇게되어 있니? –

+0

@JoachimPileborg : 문제가되지 않습니다. 문자열은 사용되지 않습니다. – wallyk

+2

파일 이름 구성이 잘못된 파일 이름을 생성하므로 결과적으로 해당 파일 이름이 열리지 않습니다. 파일이'.good()'이 아닙니다. – WhozCraig

답변

0

문제는 당신이 필요하지 않은 경우 백 슬래시 탈출을 시도하는 것이 될 수를 (컴파일해야한다). 역 슬래시를 이스케이프하는 것은 문자열 리터럴에만 필요합니다. Windows에서 여러 개의 연속 된 백 슬래시가있는 경로가 마음에 들지 않을 수도 있습니다.

+0

"더 간단한"버전 (불필요한'CStringA' ->'const char *'난센스를 제거하기 위해 조금 다듬어 짐)은 저에게 잘 맞습니다. 어떤 증상을 관찰하고 있습니까? 'ifstream :: is_open'이 true를 리턴합니까? –

+0

@Nik 잘 코드 파일의 첫 번째 블록에서 CString을 저장하는 CFileDialog를 사용하여 파일 이름을 검색했기 때문에 변환을 했으므로 함수에 전달했습니다. ifstream은 CString을 const char * 또는 atleast로 받아들이지 않습니다. 또한 is_open이 작동하는 것처럼 보이므로 무한 루프에 걸릴 수 있습니다. 그렇습니까? – user1704863

+0

시계가 추가되면 브레이크 포인트가 추가됩니다. while 루프는 getline()을 수행 한 다음 루프를 종료하고 ""을 recText에 할당합니다 – user1704863

관련 문제