typedef struct tagDATA
{
int m_nData1;
float m_fData2;
float m_fData3;
float m_fData4;
float m_fData5;
float m_fData6;
float m_fData7;
WORD m_wData8;
WORD m_wData9;
} TSTDATA;
typedef struct tagData2File
{
TSTDATA pTSTData
float fZ,fS;
float nZ,nS;
float fPos;
float fBlk;
bool bSign;
}DATA2FILE;
问题如下:
1. 将120个TSTDATA结构体的数组,先使用fwrite写入,再用fread读取,读出来的数据正确;
2. 但用相同的方法写入1200个TSTDATA的结构体,读取的数据约前150个结构正确,之后的错误;
3. 对于任何数量的DATA2FILE结构体数组,先使用fwrite写入,再用fread读取,读出来的数据就是错误。
有没有高人告知原因啊,谢谢!!!
7 个解决方案
#1
无代码无真相~
回复内容太短了!
回复内容太短了!
#2
缓冲区的问题? 没代码不好分析
#3
代码简单,就是结构体数组的IO,我想知道有人解决过这个问题没有!希望有高手解惑!
FILE *wFile;
if ((wFile = fopen(filename.c_str(), "wb")) == NULL) /*open file */
{
fprintf(stderr, "Cannot open output file.\n");
return -1;
}
//write:
const int nCount = 120;//or 1200
TSTDATA *pBuf = new TSTDATA[nCount];
//在此处对数组赋值。
int nW = fwrite(pBuf,sizeof(TSTDATA),nCount,wFile);
if(nW != nCount)
{
fprintf(stderr, "Writing incorrect.\n");
}
delete []pBuf;
//read
const int nCount = 120;//or 1200
TSTDATA *pBuf = new TSTDATA[nCount];
fread(pBuf,sizeof(TSTDATA),nCount,wFile);
//在此处调试,观察pBuf值是否正确。
delete []pBuf;
fclose(wFile);
FILE *wFile;
if ((wFile = fopen(filename.c_str(), "wb")) == NULL) /*open file */
{
fprintf(stderr, "Cannot open output file.\n");
return -1;
}
//write:
const int nCount = 120;//or 1200
TSTDATA *pBuf = new TSTDATA[nCount];
//在此处对数组赋值。
int nW = fwrite(pBuf,sizeof(TSTDATA),nCount,wFile);
if(nW != nCount)
{
fprintf(stderr, "Writing incorrect.\n");
}
delete []pBuf;
//read
const int nCount = 120;//or 1200
TSTDATA *pBuf = new TSTDATA[nCount];
fread(pBuf,sizeof(TSTDATA),nCount,wFile);
//在此处调试,观察pBuf值是否正确。
delete []pBuf;
fclose(wFile);
#4
"wb" 打开的文件不能读的……
重新用 "rb" 打开看看。
重新用 "rb" 打开看看。
#5
这个是我在下面没写,程序里用的是rb
#6
写入没问题的,读取也没问题
打开方式错了
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
打开方式错了
FILE *wFile;
if ((wFile = fopen(filename.c_str(), "r+b")) == NULL) /*open file */
{
fprintf(stderr, "Cannot open output file.\n");
return -1;
}
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
#7
//测试第3点:
//对于任何数量的DATA2FILE结构体数组,先使用fwrite写入,再用fread读取,读出来的数据就是错误。
//测试结果正确!不知道LZ的错误原因具体怎么出现的?
#include <windows.h>
#include <stdio.h>
typedef struct tagDATA
{
int m_nData1;
float m_fData2;
float m_fData3;
float m_fData4;
float m_fData5;
float m_fData6;
float m_fData7;
WORD m_wData8;
WORD m_wData9;
} TSTDATA;
typedef struct tagData2File
{
TSTDATA pTSTData;
float fZ,fS;
float nZ,nS;
float fPos;
float fBlk;
bool bSign;
}DATA2FILE;
int main()
{
FILE *fp = NULL;
DATA2FILE data = {0},datatemp = {0};
data.pTSTData.m_nData1 = 5;
data.pTSTData.m_fData2 = 2.3;
data.pTSTData.m_fData3 = 3.4;
data.pTSTData.m_fData4 = 4;
data.pTSTData.m_fData5 = 5.5;
data.fZ = 0.2;
data.fS = 3;
fp = fopen("temp.dat","wb");
if(fp == NULL) return 0;
fwrite(&data,sizeof(DATA2FILE),1,fp);
fclose(fp);
fp = fopen("temp.dat","rb");
if(fp == NULL) return 0;
fread(&datatemp,sizeof(DATA2FILE),1,fp);
fclose(fp);
printf("%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f",
data.pTSTData.m_nData1,
data.pTSTData.m_fData2,
data.pTSTData.m_fData3,
data.pTSTData.m_fData4,
data.pTSTData.m_fData5,
data.fZ,
data.fS);
}
//输出结果:5,2.30,3.40,4.00,5.50,0.20,3.00
#1
无代码无真相~
回复内容太短了!
回复内容太短了!
#2
缓冲区的问题? 没代码不好分析
#3
代码简单,就是结构体数组的IO,我想知道有人解决过这个问题没有!希望有高手解惑!
FILE *wFile;
if ((wFile = fopen(filename.c_str(), "wb")) == NULL) /*open file */
{
fprintf(stderr, "Cannot open output file.\n");
return -1;
}
//write:
const int nCount = 120;//or 1200
TSTDATA *pBuf = new TSTDATA[nCount];
//在此处对数组赋值。
int nW = fwrite(pBuf,sizeof(TSTDATA),nCount,wFile);
if(nW != nCount)
{
fprintf(stderr, "Writing incorrect.\n");
}
delete []pBuf;
//read
const int nCount = 120;//or 1200
TSTDATA *pBuf = new TSTDATA[nCount];
fread(pBuf,sizeof(TSTDATA),nCount,wFile);
//在此处调试,观察pBuf值是否正确。
delete []pBuf;
fclose(wFile);
FILE *wFile;
if ((wFile = fopen(filename.c_str(), "wb")) == NULL) /*open file */
{
fprintf(stderr, "Cannot open output file.\n");
return -1;
}
//write:
const int nCount = 120;//or 1200
TSTDATA *pBuf = new TSTDATA[nCount];
//在此处对数组赋值。
int nW = fwrite(pBuf,sizeof(TSTDATA),nCount,wFile);
if(nW != nCount)
{
fprintf(stderr, "Writing incorrect.\n");
}
delete []pBuf;
//read
const int nCount = 120;//or 1200
TSTDATA *pBuf = new TSTDATA[nCount];
fread(pBuf,sizeof(TSTDATA),nCount,wFile);
//在此处调试,观察pBuf值是否正确。
delete []pBuf;
fclose(wFile);
#4
"wb" 打开的文件不能读的……
重新用 "rb" 打开看看。
重新用 "rb" 打开看看。
#5
这个是我在下面没写,程序里用的是rb
#6
写入没问题的,读取也没问题
打开方式错了
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
打开方式错了
FILE *wFile;
if ((wFile = fopen(filename.c_str(), "r+b")) == NULL) /*open file */
{
fprintf(stderr, "Cannot open output file.\n");
return -1;
}
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
#7
//测试第3点:
//对于任何数量的DATA2FILE结构体数组,先使用fwrite写入,再用fread读取,读出来的数据就是错误。
//测试结果正确!不知道LZ的错误原因具体怎么出现的?
#include <windows.h>
#include <stdio.h>
typedef struct tagDATA
{
int m_nData1;
float m_fData2;
float m_fData3;
float m_fData4;
float m_fData5;
float m_fData6;
float m_fData7;
WORD m_wData8;
WORD m_wData9;
} TSTDATA;
typedef struct tagData2File
{
TSTDATA pTSTData;
float fZ,fS;
float nZ,nS;
float fPos;
float fBlk;
bool bSign;
}DATA2FILE;
int main()
{
FILE *fp = NULL;
DATA2FILE data = {0},datatemp = {0};
data.pTSTData.m_nData1 = 5;
data.pTSTData.m_fData2 = 2.3;
data.pTSTData.m_fData3 = 3.4;
data.pTSTData.m_fData4 = 4;
data.pTSTData.m_fData5 = 5.5;
data.fZ = 0.2;
data.fS = 3;
fp = fopen("temp.dat","wb");
if(fp == NULL) return 0;
fwrite(&data,sizeof(DATA2FILE),1,fp);
fclose(fp);
fp = fopen("temp.dat","rb");
if(fp == NULL) return 0;
fread(&datatemp,sizeof(DATA2FILE),1,fp);
fclose(fp);
printf("%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f",
data.pTSTData.m_nData1,
data.pTSTData.m_fData2,
data.pTSTData.m_fData3,
data.pTSTData.m_fData4,
data.pTSTData.m_fData5,
data.fZ,
data.fS);
}
//输出结果:5,2.30,3.40,4.00,5.50,0.20,3.00