C语言读取文件,查找字符。。。。(高手帮忙看看,小弟搞了好几天了)
这代码帮我看看那里有错,我是想读取了一个文件找出了tmp1,和tmp2然后打开另一个文件,在里面查找这个,找到了就输出这一行的内容,但是这样写代码好像不行 啊。。#include "stdio.h"
#include "stdlib.h"
#include <string.h>
struct info
{
char name[10];
char guojia[50];
char chengshi[80];
float x;
float y;
char shiqu[3];
}weater_test[9000];
void main()
{
FILE *fileout;
FILE *fileout_id;
char test_char[123];
int lenght ,i,j;
char tmp1[123];
char tmp2[123];
fileout = fopen("weather","r");
if(fileout == NULL)
{
printf("open file false\n");
exit(1);
}
lenght=fread(test_char,1,sizeof(test_char),fileout);
fclose(fileout);
printf("length=%d\n",lenght);
for(i=0;i<lenght;i++)
{
if((test_char[i]=='g')&&(test_char[i+1] == '('))
j = i+2;
if(test_char[i] == ',')
{
memcpy(tmp1,test_char+j,i-j);
printf("%s\n",tmp1);
break;
}
}
for(i=0;i<lenght;i++)
{
if((test_char[i]==',')&&(test_char[i+1] == ' '))
j = i+2;
if((test_char[i] == ')')&&(test_char[i+1] == ','))
{
memcpy(tmp2,test_char+j,i-j);
printf("%s\n",tmp2);
break;
}
}
fileout_id=fopen("weather_id.txt","r");
if(fileout_id == NULL)
{
printf("open fileout_id error\n");
exit(1);
}
for(i =0; feof(fileout_id) != 0; i++ )
fscanf( fileout_id, "%s,%s,%s,%f,%f,%s", weater_test[i].name, weater_test[i].guojia, weater_test[i].chengshi, &weater_test[i].x, &weater_test[i].y, weater_test[i].shiqu );
fclose(fileout_id);
// for(i=0;(weater_test[i].x== *tmp1 && weater_test[i].y== *tmp2);i++);
// printf( "%s,%s,%s,%f,%f,%s", weater_test[i].name,weater_test[i].guojia, weater_test[i].chengshi, &weater_test[i].x, &weater_test[i].y, weater_test[i].shiqu );
}
[ 本帖最后由 cz737 于 2010-7-16 15:46 编辑 ]