需要解释 wchar_t* 到二进制文件的读写
有人可以解释一下将 wchar_t* 字符串写入二进制文件然后读回的正确方法是什么(使用 fread/fwrite)吗?
这就是我所拥有的(其工作)
struct someStruct{
int someint;
wchar_t* data;
int someint2;
}
someStruct s;
s.someint = 111;
s.data = L"blah blah .... lets say 1-1000 characters long";
s.someint2 = 222;
//writing
FILE * f = _wfopen(L"myfile",L"wb");
fwrite(&(s.someint), sizeof(int), 1, f);
fwrite(&(s.data), sizeof(wchar*), 1, f);
fwrite(&(s.someint2), sizeof(int), 1, f);
fclose(f);
//reading
FILE * f2 = _wfopen(L"myfile",L"rb");
fread(&(s2.someint), sizeof(int), 1, f2);
fread(&(s2.data), sizeof(wchar*), 1, f2);
fread(&(s2.someint2), sizeof(int), 1, f2);
fclose(f2);
一切正常,值已正确加载。 问题是这个特定示例中 fread 和 fwrite 的第二个参数应该是什么,为什么它与例如 4(即 sizeof(wchar_t*))或 20 一起工作并导致缓冲区溢出 150,这些值根据数据长度而变化,
编辑:这些是我一直在使用的(上次我检查它时正在工作:P 1年前):
wchar_t* loadStrFromFile(FILE* file) {
int strLen;
fread(&(strLen), sizeof(int), 1, file);
wchar_t* result = new wchar_t[strLen];
fread(result, strLen, sizeof(wchar_t), file);
return result;
}
void saveStrToFile(const wchar_t*& data, FILE* file) {
int strLen = wcslen(data)+1;
fwrite(&strLen, sizeof(int), 1, file);
fwrite(data, strLen, sizeof(wchar_t), file);
}
can someone explain me what is a proper way to write wchar_t* string to binary file and then read it back(using fread/fwrite)?
here is what i have (its working)
struct someStruct{
int someint;
wchar_t* data;
int someint2;
}
someStruct s;
s.someint = 111;
s.data = L"blah blah .... lets say 1-1000 characters long";
s.someint2 = 222;
//writing
FILE * f = _wfopen(L"myfile",L"wb");
fwrite(&(s.someint), sizeof(int), 1, f);
fwrite(&(s.data), sizeof(wchar*), 1, f);
fwrite(&(s.someint2), sizeof(int), 1, f);
fclose(f);
//reading
FILE * f2 = _wfopen(L"myfile",L"rb");
fread(&(s2.someint), sizeof(int), 1, f2);
fread(&(s2.data), sizeof(wchar*), 1, f2);
fread(&(s2.someint2), sizeof(int), 1, f2);
fclose(f2);
everything is working, values are properly loaded. the question is what should be the second parameter of fread and fwrite in this specific example, why is it working with e.g. 4(which is sizeof(wchar_t*)) or 20 and causing buffer overrun with 150, these values vary based on data length,
EDIT: these are what i have been using (was working last time i checked it :P 1 year ago):
wchar_t* loadStrFromFile(FILE* file) {
int strLen;
fread(&(strLen), sizeof(int), 1, file);
wchar_t* result = new wchar_t[strLen];
fread(result, strLen, sizeof(wchar_t), file);
return result;
}
void saveStrToFile(const wchar_t*& data, FILE* file) {
int strLen = wcslen(data)+1;
fwrite(&strLen, sizeof(int), 1, file);
fwrite(data, strLen, sizeof(wchar_t), file);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是特别错误的:
这只会写入 sizeof(wchar*) = 4 或 8 字节。
哦,是的,你可能不想写一个指针。错过了那个。
如果您想写入实际的字符串数据(假设您使用的是 Windows):
This is particularly wrong:
This would only write sizeof(wchar*) = 4 or 8 bytes.
Oh yes, and you probably don't want to write a pointer. Missed that one.
If you want to write an actual string data (assuming you're on Windows):