fopen() failing
fopen() failing
我有一个C++程序,可以在Windows xp启动时将文件与远程服务器同步。需要打开公钥文件的函数在 fopen() 失败。当我自己启动程序(从资源管理器中)时,一切正常。但是当我将启动密钥添加到注册表时,该功能失败。
我通过调试器跟踪代码,一切都很好,直到调用 CreateFileA()。创建文件A 返回FILE_NOT_FOUND。
我删除了对 fopen() 的调用,并将其替换为直接对 CreateFileA() 的调用。然后我SECURITY_ATTRIBUTES更改为 NULL,之后对 CreateFileA() 的调用起作用。
问题是我用于加密的第三方库需要一个 FILE* 对象,而不仅仅是从文件中读取的数据。如何解决我的问题?
这是我当前使用的代码:
if( !GetModuleFileNameA(NULL, Path, MAX_PATH) ){
delete [] buf;
delete [] Path;
return strerror( errno );
}
rPath = Path;
delete [] Path;
ret = rPath.find_last_of( '' );
if( ret == string::npos ){
delete [] buf;
return strerror( errno );
}
ret++;
rPath.erase( rPath.begin() + ret, rPath.begin() + rPath.size() - ret );
rPath += "rsa_pub.txt";
if( ( f = fopen( rPath.c_str(), "rb" ) ) == NULL ){ // fails when started from registry
delete [] buf;
return strerror( errno );
}
编辑:
我找到了这个问题的黑客解决方案:如果我释放运行时库然后重新加载它,问题就会消失。但是,这不是一个非常优雅的解决方案。是否可以在不删除和重新加载 dll 的情况下重置运行时?
您的rPath.erase
呼叫似乎没有多大意义
rPath.erase( rPath.begin() + ret, rPath.begin() + rPath.size() - ret );
这是应该做什么?
在这里,您使用的是erase
的(iterator, iterator)
版本。我恳求您正在尝试从位置ret
开始擦除字符串的尾部。在这种情况下,我希望它看起来像
rPath.erase( rPath.begin() + ret, rPath.end() );
如果你想使用erase
的(position, length)
版本,那么它看起来像
rPath.erase( ret, rPath.size() - ret );
但是您的特定用法看起来像是两者的奇怪混合体。你想通过这个电话做什么?
GetModuleFileNameA
可能会返回不同的字符串,具体取决于您启动程序的方式,这就是为什么您的代码在某些情况下可能看起来"有效"的原因。
相关文章:
- 试图创建流或fopen时程序崩溃
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- fopen 在 gdb 中返回 NULL
- fopen 在创建大量文件后因 EFBIG "File too large" 而失败
- CreateDepthStencilView() is failing
- fopen 与常量字符 * 从 QString 是不可能的
- 在C++中打开的带有"fopen"的mmap文件
- 无法使用 fopen() 加载文件
- 在我可以从fopen打开文件时,CopyFile无法找到该文件
- 使用 fopen() 的最佳方法是什么
- 使用MMAP与FSTREAM或FOPEN访问二进制文件
- 使用 fopen 而不是 Visual Studio 建议的 fopen_s 有什么错误
- 'fopen'返回格式错误的'fp'
- fopen 和 fwrite 从多个线程到同一个文件
- fopen 几分钟后无法打开文件
- CGAL static_cast failing
- 对"fopen"的期望
- header for fopen visual studio 2015 community edition
- 在UWP中获取fopen()的应用程序路径
- fopen() failing