filldata2[1][1].0.c

来自「地震数据处理中」· C语言 代码 · 共 198 行

C
198
字号
//地震数据处理中,将高程数据提取出来,变成格式相同的数据文件

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//atoi(),atof()

typedef struct{
char id[20];
float x;
float y;
float h;
}Record;


void initData(Record *data);
int ReadData(FILE *fp,FILE *fp2,Record *data1,Record *data2);
void FillData(FILE *fp,Record *data1,Record *data2);
int stringtoint(char *string);
float stringtofloat(char *string);
int getid(char *string);
int getbid(char *string);

void initData(Record *data){
	
	data->id[0]='\0';
	data->x=0.000;
	data->y=0.000;
	data->h=0.000;
}

int stringtoint(char *string){
	return  atoi(string);
	
}
float stringtofloat(char *string){
	//if(strchr(string,'.')){
	return atof(string);
	//}
	
}
int getid(char *string)
	{
	  char seps[]="-";
	  char *token;
	
 	  token = strtok(string, seps );
	  /* Get next  token: */
	  token = strtok( NULL, seps );	
	  return stringtoint(token);
 	 }
int getbid(char *string)
	{
	  char seps[]="-";
	  char *token;
	
 	  token = strtok(string, seps );
	  return stringtoint(token);
 	 }
int ReadData(FILE *fp,FILE *fp2,Record *data1,Record *data2){
	int id1,id2;
	char string[100],str[20];
	char seps[] = " ,";
	char *token;

	//读第一个数据
      	if(strlen(data1->id)==0){
		//########################
		//当时错误:过滤\n
		//########################
		if(fscanf(fp,"%[^\n]%*c",string)!=EOF){
	
		printf("read data1 from from.txt success.\n");
	

	  token = strtok( string, seps );
	  strcpy(data1->id,token);
 	 	
	/* Get next 2 tokens: */
	token = strtok( NULL, seps );
	token = strtok( NULL, seps );
	  //高程
	token = strtok( NULL, seps );
	data1->h=stringtofloat(token);
	
	//输出第一个数据
	if(fprintf(fp2,"%s,,%.3f,%.3f,%.3f\n",data1->id,data1->x,data1->y,data1->h)<0)
	printf("write data1 error\n");
	else
	printf("write data1 to to.txt success\n");

		}
	}
	//########################
	//当时错误:位置错误
	//########################
	strcpy(str,data1->id);
	id1=getid(str);
	
	
	do {
	if(fscanf(fp,"%[^\n]%*c",string)!=EOF){
	
	printf("read data2 from from.txt success.\n");
	token = strtok( string, seps );

	if( token != NULL )
	  strcpy(data2->id,token);
 	  
	
	/* Get next 2 tokens: */
	token = strtok( NULL, seps );
	token = strtok( NULL, seps );
	  //高程
	token = strtok( NULL, seps );
	data2->h=stringtofloat(token);
	
	strcpy(str,data2->id);
	id2=getid(str);
	
	}
	else 
	return 0; 
	//printf("%d.....%d\n",id1,id2);getch();
	}while(id2<=id1);//重读第2次的数据

	return 1;
}
void FillData(FILE *fp,Record *data1,Record *data2){
	int cha1;
	float cha2;
	char seps[] = "-";
	char *token,str[20];
	int id1,id2,bid;
	
	strcpy(str,data1->id);
	bid=getbid(str);
	strcpy(str,data1->id);
     	id1=getid(str);
	strcpy(str,data2->id);
	id2=getid(str);
	
	cha1=id2-id1;
	cha2=data2->h-data1->h;
	
	while(id1!=id2-1){
		//########################
		//当时错误:id1++
		//########################
		id1++;
		data1->h+=cha2/cha1;
		//输出计算得到的数据
		if(fprintf(fp,"%d-%d,,%.3f,%.3f,%.3f\n",bid,id1,data1->x,data1->y,data1->h)<0)
		printf("write data error");
		else
		printf("write inserted data to to.txt success\n");
	}

	//输出第2次读到的数据
	if(fprintf(fp,"%s,,%.3f,%.3f,%.3f\n",data2->id,data2->x,data2->y,data2->h)<0)
	printf("write data2 error\n");
	else
	printf("write data2 to to.txt success\n");
	
	//交换数据
	*data1=*data2;
	
}
void main(){
FILE *from,*to;
Record data1,data2;	
//初始化,否则tc下出问题
char *from_files="d:\\from.txt",*to_files="d:\\to.txt";
clrscr();
initData(&data1);initData(&data2);

printf("INPUT FILES: FOR EXAMPLE d:\\from.txt,d:\\to.txt\n>>>");
while(scanf("%[^,\n]%*c%s",from_files,to_files)!=2)fflush(stdin);
if((from=fopen(from_files,"r"))==NULL){
		printf("open from.txt files error");
		exit(1);}
	else printf("open from.txt success.\n");
if((to=fopen(to_files,"w"))==NULL) {
		printf("open to.txt files error");
		exit(1);
	}
	else printf("open to.txt success.\n");
	
while(ReadData(from,to,&data1,&data2)){
FillData(to,&data1,&data2);
}

getch();
fclose(from);
fclose(to);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?