我的代碼在此處讀取文件的路徑,對其進行修改(如果找到反斜槓,請添加額外的反斜槓),並將其寫回另一個文件。但結果是一個小錯誤。讀取文件在字符之間找到空格後結束
例如運行:
文本文件A - >c:\program files\work\exe
文本文件乙 - >c:\\program
,我期望能夠在文本文件B中的結果是這樣的:
文本文件A - >c:\\program files\\work\\exe
//read path data from text file
char str[256];
fstream file_op("C:\\PROGRA~1\\logdata",ios::in);
file_op >> str;
file_op.close();
char newPath[MAX_PATH];
int newCount = 0;
for(int i=0; i < strlen(str); i++)
{
if(str[i] == '\\')
{
newPath[newCount++] = str[i];
}
newPath[newCount++] = str[i];
}
newPath[newCount]=0;
ofstream out("c:\\path.txt", ios::out | ios::out);
out.write(newPath, strlen(newPath));
out.close();
解決了。我用這個 - > file_op.getline(str,256); – karikari 2011-03-01 09:13:01