首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C getopenfilename getFileName

C getopenfilename getFileName
EN

Stack Overflow用户
提问于 2013-04-26 11:03:28
回答 1查看 641关注 0票数 0

我有下面的代码,问题是,当我打印文件的完整路径名时,数组中的每个字符之间都有两个空格。

代码语言:javascript
复制
// initialization outside any class in .c code
OPENFILENAME ofn;       // common dialog box structure
char szFile[260];       // buffer for file name
HWND hwnd;              // owner window
HANDLE hf;              // file handle
...
...
// inside a function
initializeOpenFile();
GetOpenFileName(&ofn);


            for(i = 0; i < sizeof(szFile)/sizeof(char);i++){
                fprintf(stderr,"%c", szFile[i]);
                }
        }
}

void initializeOpenFile(){
    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = szFile;
    // Set lpstrFile[0] to '\0' so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = sizeof(szFile);
    ofn.lpstrFilter = TEXT("All\0*.*\0Text\0*.TXT\0");
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
}

打印我:

我想使用要传递给openFile函数的char数组:

代码语言:javascript
复制
FILE* fp = fopen( filename, "r" );
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-26 12:14:10

它看起来像是一根宽线,即。一串宽字符。(参考:wikipedia)

所以应该声明szFile:

代码语言:javascript
复制
wchar_t szFile[260];

然后你可以转换它(我想!)和wcstombs()在一起。

代码语言:javascript
复制
char szPath[260];
wcstombs(szPath, szFile, 260);

szPath现在应该包含一个“普通”(窄)字符串。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16228060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档