SOS!关于数据读写中字节的问题

pdexcl 2003-09-29 11:51:28
我先把数组中的数(注意是int类型的)写入到文件中,再把这个文件中的内容写入到数组中,可是本来我有5个数,从文件中读出来之后成7个数了,字节问题怎么处理,比如1和1000应该占用相同的字节,长度应该是相同的,用我以下的方法做了之后,长度是不同的,比如1的长度就是1,1000的长度就是4了,从文件里面读出的时候就是一个一个的读出的,1000就成了1、0、0、0了,请问大侠怎么处理这样的问题,谢谢各位了!
以下是我的代码:void __fastcall TForm1::Button1Click(TObject *Sender)
{
//将数组中的内容写入文件
int array[5]={10,2,35,4,5};
int i;
int iFileHandle;
int iLength;

iFileHandle=FileOpen("f:\\xcl\\课题模块\\text1.txt",fmOpenWrite);

for (i=0;i<5;i++)
{
AnsiString Test = IntToStr(array[i]);
int len=Test.Length();
FileWrite(iFileHandle, Test.c_str(), len);
}
FileClose(iFileHandle);
}


void __fastcall TForm1::Button2Click(TObject *Sender)
{
//从文件里面读到数组里
int iFileHandle,iBytesRead,iFileLength;
int i,Y=10,del_y=15;
char *pszBuffer;
iFileHandle=FileOpen("f:\\xcl\\课题模块\\text1.txt",fmOpenRead); //打开文件
iFileLength=FileSeek(iFileHandle,0,2); //检查文件长度
pszBuffer=new char[iFileLength+1]; //设置虚拟空间为文件长度+1
FileSeek(iFileHandle,0,0);
iBytesRead=FileRead(iFileHandle,pszBuffer,iFileLength);
FileClose(iFileHandle);
for(i=0;i<iBytesRead;i++)
{
Canvas->TextOut(10,Y+del_y,StrToInt(pszBuffer[i]));
Y=Y+del_y;
}
delete [] pszBuffer;
}
...全文
34 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljianq 2003-09-29
  • 打赏
  • 举报
回复
参靠(以下已调试通过):

void __fastcall TForm1::Button1Click(TObject *Sender)
{
int array[5]={10,2,35,4,5};
int i;
TFileStream* mFile=NULL;
try {
mFile=new TFileStream("f:\\xcl\\课题模块\\text1.dat",fmCreate);
for (i=0;i<5;i++)
mFile->Write(&array[i], sizeof(int));
}
__finally {
delete mFile;
mFile=NULL;
}
}


//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//从文件里面读到数组里
int iFileHandle,iBytesRead,iFileLength;
int i,Y=10,del_y=15;
int pszBuffer;
TFileStream* mFile=NULL;
try {
mFile=new TFileStream("f:\\xcl\\课题模块\\text1.dat",fmOpenRead); //打开文件
FileSeek(iFileHandle,0,0);
iBytesRead=mFile->Read(&pszBuffer,sizeof(int));
while(iBytesRead==sizeof(int))
{
Canvas->TextOut(10,Y+del_y,AnsiString(pszBuffer));
Y=Y+del_y;
iBytesRead=mFile->Read(&pszBuffer,sizeof(int));
}
}
__finally{
delete mFile;
mFile=NULL;
}
}
ljianq 2003-09-29
  • 打赏
  • 举报
回复
你应该写入分隔符;
pdexcl 2003-09-29
  • 打赏
  • 举报
回复
谢谢ljianq()!高手啊,OK!呵呵,要不然这么多三角形,快升星了吧,可怜我还是一个三角形,是不是回答别人的问题多了,就升级。I have a long way to go!
马上给您结分!

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧