| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1093 人关注过本帖
标题:数据读取的问题
只看楼主 加入收藏
tb361
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-6-5
收藏
 问题点数:0 回复次数:1 
数据读取的问题
如何将文本文件(.txt)中的数据一次读入到一个已定义的数组中呢?
请各位大虾帮帮忙啊,在线等
给几条具体程序最好不过了。
谢谢啊!~!
搜索更多相关主题的帖子: 数据 
2008-06-05 19:12
windlzf
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-8-7
收藏
得分:0 
BCB F1帮助就有例子啊
The following example uses a button, a string grid, and an Open dialog box on a form. When the button is clicked, the user is prompted for a filename. When the user clicks OK, the specified file is opened, read into a buffer, and closed. Then the buffer is displayed in two columns of the string grid. The first column contains the character values in the buffer. The second column contains the numeric values of the characters in the buffer.

void __fastcall TForm1::Button1Click(TObject *Sender)

{
  int iFileHandle;
  int iFileLength;
  int iBytesRead;
  char *pszBuffer;
  if (OpenDialog1->Execute())
  {
    try
    {
      iFileHandle = FileOpen(OpenDialog1->FileName, fmOpenRead);
      iFileLength = FileSeek(iFileHandle,0,2);
      FileSeek(iFileHandle,0,0);
      pszBuffer = newchar[iFileLength+1];
      iBytesRead = FileRead(iFileHandle, pszBuffer, iFileLength);
      FileClose(iFileHandle);

      for (int i=0;i<iBytesRead;i++)
      {
        StringGrid1->RowCount += 1;
        StringGrid1->Cells[1][i+1] = pszBuffer[i];
        StringGrid1->Cells[2][i+1] = IntToStr((int)pszBuffer[i]);
      }
      delete [] pszBuffer;
    }
    catch(...)
    {
      Application->MessageBox("Can't perform one of the following file operations: Open, Seek, Read, Close.", "File Error", IDOK);
    }
  }
}

改一改就可以啦:

int ReadTxtFile(char *fileName)
{
        int iFileHandle;
        int iFileLength;
        int iBytesRead;
        char *pszBuffer;

        iFileHandle = FileOpen(fileName, fmOpenRead);
        if(iFileHandle==-1) {ShowMessage("Can't open file");return 0;}
        iFileLength = FileSeek(iFileHandle,0,2);
        FileSeek(iFileHandle,0,0);
        pszBuffer = new char[iFileLength+1];
        iBytesRead = FileRead(iFileHandle, pszBuffer, iFileLength);//读取出来了
        //使用吧
        delete []pszBuffer;//用完了释放掉
        FileClose(iFileHandle);
        return 1;
}
2008-06-06 16:36
快速回复:数据读取的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017338 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved