用c++builder读取一个一行有多行变量的文件

本文介绍如何使用C++读取并解析特定格式的输入文件,包括使用fstream库进行文件操作,通过getline和>>操作符读取并处理每一行数据,最终将头两个变量展示在CheckListBox中。

文件内容如下:

C DXDY.INP FILE, IN FREE FORMAT ACROSS COLUMNS for  83658 Active Cells
C 2013-5-25   上午 10:43:53
C                                           BOTTOM                 Veg
C  I    J        DX        DY      DEPTH     ELEV     ZROUGH      TYPE
    3  143     221.500   187.900     0.090    22.000    0.0200
    3  144     222.000   188.100     0.090    22.000    0.0200
    3  145     222.200   188.100     0.090    22.000    0.0200
    4  143     223.100   187.900     0.090    22.000    0.0200
    4  144     223.300   187.900     0.090    22.000    0.0200
    4  145     223.500   187.800     0.090    22.000    0.0200
    5  142     222.400   187.900     0.090    22.000    0.0200
    5  143     222.500   187.900     0.090    22.000    0.0200
    5  144     222.700   187.800     0.090    22.000    0.0200
    6  140     222.300   188.600     0.090    21.154    0.0200
    6  141     222.500   188.700     0.090    22.000    0.0200
    6  142     222.800   187.900     0.090    22.000    0.0200
    6  143     222.900   187.800     0.090    22.000    0.0200

    ...........

文件共83650行,采用以下方式读取,并将读取的每行头两个变量显示在CheckListBox中:

//---------------------------------------------------------------------------

#include <vcl.h>
#include <fstream.h>
#include <string.h>
#pragma hdrstop

#include "readlxly.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  this->OpenDialog1->FileName ="*.inp";
  this->OpenDialog1->Title="打开lxly.inp文件";
  if(this->OpenDialog1->Execute())
  {
     string str,str1,str2,str3,str4,str5,str6,str7;
     ifstream inf(this->OpenDialog1->FileName.c_str());
     for(int i=1;i<=4;i++)
     {
        getline(inf,str);
        this->CheckListBox1->Items->Add(str.c_str());
     }
     this->ProgressBar1->Min =5;
     this->ProgressBar1->Max =83650;
     int k=5;
     TStringList *ttt=NULL;
     ttt=new TStringList();

     while(!inf.eof())
     {
       inf>>str1>>str2>>str3>>str4>>str5>>str6>>str7;
       ttt->Add((str1+" "+str2).c_str());
       k++;
       this->ProgressBar1->Position=k;
       }
       this->CheckListBox1->Items =ttt;
     inf.close();
  }
}
//---------------------------------------------------------------------------

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值