C ++中的路径/文件名斜杠

时间:2015-05-12 16:47:45

标签: c file

您好我的程序中有一点问题

FILE* f;
char * psz = new char[wcslen(file) + 1];
wsprintfA(psz, "%S", file);
fopen_s(&f,psz, "w+b");
char * buffer = "data\heightmap.bmp";
 fwrite(buffer,256, 1, f);
fclose(f); 

//later in another class:
FILE* f;
char * bufer;
fopen_s(&f,"data\test.dat", "rb");
fread(buffer,256, 1, f);
fclose(f);
///buffer is "dataheightmap.bmp" not "data\heightmap.bmp" as it should be to open the file later.

FILE* f;
fopen_s(&f,buffer, "rb");
count = fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, f);
fclose(f);
有人可以帮助我。 谢谢 Window7,visual studio 2013。

5 个答案:

答案 0 :(得分:1)

请记住,你需要逃避斜线:

"data\heightmap.bmp"
"data\test.dat"

应该是:

"data\\heightmap.bmp"
"data\\test.dat"

答案 1 :(得分:0)

您的专栏:char * buffer = "data\heightmap.bmp";

应该是:char * buffer = "data\\heightmap.bmp";

同样的事情:fopen_s(&f,"data\test.dat", "rb");

应该是:fopen_s(&f,"data\\test.dat", "rb");

答案 2 :(得分:0)

你需要在C字符串常量中转义字符'\'。使用以下声明。

char * buffer =“data \\ heightmap.bmp”;

答案 3 :(得分:0)

好的,我发现在我之前使用它

case WM_COMMAND:
        if (LOWORD(wparam) == ID_CREATE_HEIGHTMAP){
        TCHAR szFile[_MAX_PATH] = L"";
        OPENFILENAME ofn = { 0 };
        ofn.lStructSize = sizeof(OPENFILENAME);
        ofn.hwndOwner = System->hwnd;
        ofn.lpstrFilter = L"Heightmap data File (*.hdf)\0*.hdf\0\0";
        ofn.lpstrFile = szFile;  // This will hold the file name
        ofn.lpstrDefExt = L"hdf";
        ofn.nMaxFile = _MAX_PATH;
        ofn.lpstrTitle = L"Save File";
        ofn.Flags = OFN_OVERWRITEPROMPT;
        GetSaveFileName(&ofn);
        if (szFile[0]!=0)
            System->Createype(3, szFile);
    }
        break;

仅当我将文件夹放在此文件夹(GetSaveFileName(& ofn))中时才有效。

答案 4 :(得分:0)

谢谢,我添加了OFN_NOCHANGEDIR,现在似乎有用了。