我想弄明白這一點,它現在已經很久了。fstream .open()Win32
當我鍵入:
inFile.open("C:\Users\Mark\Desktop\text.txt", ios::in | ios:: binary);
它工作得很好。 但是當我做這樣的事情。
string garbage = "\\";
srcLoc = ofn.lpstrFile; // this is: C:\Users\Mark\Desktop\text.txt
// This for loop inserts "\\"
for(int i = 0; i < srcLoc.length(); i++)
{
switch(srcLoc[i])
{
case '\\':
srcLoc.insert(i, garbage);
i++;
break;
}
}
// Now string srcLoc looks like: C:\\Users\Mark\\Desktop\\text.txt
inFile.open(srcLoc.c_str(), ios::in | ios:: binary);
// But it wont work
if(inFile)
{
while(!inFile.eof())
{
getline(inFile, tekst);
SendMessage(hTextBox, EM_REPLACESEL, 0, (LPARAM)tekst.c_str());
SendMessage(hTextBox, EM_REPLACESEL, 0, (LPARAM)"\r\n");
}
}
else
{
MessageBox(0, srcLoc.c_str(), "Could not load", MB_ICONWARNING | MB_OK);
}
inFile.close();
我所得到的消息框「無法加載」的工作至少:)任何人都知道我缺少什麼?
你應該只需要在字符串中加雙反斜槓,而不是已經正常的變量。 – 2012-03-28 18:34:36
當您在文字字符串中使用'/'編碼文件路徑時,它也可以在Windows上運行,然後您不需要轉義所有反斜槓。 – Praetorian 2012-03-28 18:58:44