读取文件,先找到文件的位置,然后判断文件读取的是否成功
AnsiString allsql = "";
FILE* fp = NULL;
char strtn[12000];
AnsiString filePath = "\\ZZF_price.bcp"; //找当前磁盘根目录下的文件
if ((fp = fopen(filePath.c_str(), "r"))== NULL)
{
ShowMessage("读取文件失败");
return;
}
int i=0,flag;
while(fgets(strtn, 12000, fp) != NULL) //fgets函数是从字符串流中读取数据,直到文件的结束标志EOF或有换行符(windows下是“\r\n”,Liunx下是“\n”,Mac下是"\r"),并把 读 到的字符串流读到strtn中。
{
AnsiString oneline = strtn;
// Memo1->Text=oneline;
String value[18];
int index = 0;
flag = 0; //默认flag值为0
int pos=oneline.Pos("\t");//字符流中的字段结束符,如果对应这种结束符,不等于0且为这个字段的长度(含“\t”),否则等于0;
while(pos>0)
{
// ShowMessage(pos);
value[index] = oneline.SubString(1,pos-1);//SubString是从beginindex开始,取pos-1个字符。
//ShowMessage(value[index]);
//ShowMessage(index);
oneline.Delete(1,pos);//从oneline字符串删除一个字段
if(0!=oneline.Pos("\t"))
pos=oneline.Pos("\t");
else
pos=oneline.Pos("\n");//检测是不是行结尾。
//ShowMessage(pos);
index++;
}
String intime= value[0]; //取出分割好的字符串
String train_no= value[1];
String train_code= value[2];
String station_train_code= value[3];
String station_no= value[4];
String station_name=value[5];
String distance=value[6];
String route_name=value[7];
String arrive_time=value[8];
String start_time=value[9];
String seat_type_code=value[10];
String seat_type_name=value[11];
String ticket_price=value[12];
int distance1=StrToInt(distance);
float ticket_price1=StrToFloat(ticket_price);
short distance2=(short)distance1;
double ticket_price2=(double)ticket_price1;
ShowMessage(intime+"\t"+train_no+"\t"+train_code+"\t"+station_train_code+"\t"+
station_no+"\t"+station_name+"\t"+distance+"\t"+route_name+"\t"+arrive_time+"\t"+
start_time+"\t"+seat_type_code+"\t"+seat_type_name+"\t"+ticket_price);
fclose结束文件的读取。