用createfile,writefile保存数据到txt文件,出现乱码 - C++ Builder / Windows SDK/API
我是要把double型的数据写到txt文件中,代码如下:
hfile=CreateFile("F:\\1.txt",GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
char *d=new char[5];
for(...)
{
....省略了数据的计算过程,dbValue是double型的数据
memcpy(d,&dbValue,sizeof(double));
WriteFile(hfile,(LPSTR)&d,sizeof(d[5]),&dwWrites,NULL);
SetFilePointer(hfile,2,NULL,FILE_END);
}
CloseHandle(hfile);
但是这样出来是乱码。
[解决办法]
给你个简单例子:
char *p = new char[20];memset(p, 0x0, 20);double dbValue = 1234.56;sprintf(p, "%.2f", dbValue);DWORD dwWrites;WriteFile(hfile, p, strlen(p), &dwWrites,NULL);delete []p;
[解决办法]
CBC 帮助
FILE *stream;
int i = 100;
char c = 'C';
float f = 1.234;
/* open a file for update */
stream = fopen("1.txt", "w+");
/* write some data to the file */
fprintf(stream, "%d %c %f", i, c, f);
/* close the file */
fclose(stream);