my question about read file !

Y

yezi

Hi, ALl:

The following code is to canculate 2 vector distance. Suppose the
vectore is stored in some txt file like

-1 0.34
0 0.045
1 0.98
1 0.01

My question for this txt file , if u use the vi or gedit, last line is
empty, and by default inserted into to the txt file.

The code I canculated the distance:

1> Read the vector from the file
2> compute the distance.

What I found is:

IF the file is edited by vi or gedit. The result is always false.
However if I use emacs to elimit the last empty line, then the code
work fine. My question, how can ask my code to deal with them (vi and
gedit) with same result as the emacs?

Thanks for any comments.



int main(int argc, char *argv[])
{
double difference=0.0;
float number1=0.0;
float number2=0.0;
int in1,in2;
int findFlag=0;

FILE *fp1;
FILE *fp2;

// Judge the input format
if( argc < 3) {
printf(" Enter <program> <RD.File 1> <RD.file 2>\n");
exit(1);
}

//Open the RD.File1
fp1 = fopen(argv[1],"r");
if(fp1 == NULL){
printf ("can not open %s \n",argv[1]);
exit(1);
}

//open the RD.File2
fp2 = fopen(argv[2],"r");
if(fp2 == NULL){
printf ("can not open %s \n",argv[2]);
exit(1);
}

while(!feof(fp1)){
//compare the difference
findFlag=0;
fscanf(fp1, "%d %f", &in1, &number1);
// printf("fp1 %d,%f\n",in1,number1);
// printf("------------------------\n");
rewind(fp2);
while(!feof(fp2)){
fscanf(fp2,"%d %f", &in2, &number2);
// printf("fp2 %d,%f\n",in2,number2);
if (in1==in2)
{
difference = difference +(number1-number2)*(number1-number2);
// printf(" difference is %e\n", difference);
findFlag = 1; //find the item in the second file
}
}

if (findFlag ==0){
// printf("fp2 %d,%f\n",in2,number2);
difference = difference + number1*number1;
// printf(" find FLag =0 ,difference is %e\n", difference);
// printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
} // don not find the item in the second file
// printf("----------------------\n");
}

// find the only item in the file 2
rewind(fp1);
rewind(fp2);
while(!feof(fp2)){
fscanf(fp2,"%d %f", &in2, &number2);
findFlag = 0;
rewind(fp1);
while(!feof(fp1)){
fscanf(fp1,"%d %f", &in1, &number1);
if (in2==in1)
{
findFlag = 1; //find the item in the second file
}
}
if (findFlag ==0){
difference = difference + number2*number2;
// printf(" difference is %e\n", difference);
}
}

fclose(fp1);
fclose(fp2);
difference = sqrt(difference);
printf(" %e \n" ,difference);

return 0;
}
 
J

Jordan Abel

Hi, ALl:

The following code is to canculate 2 vector distance. Suppose the
vectore is stored in some txt file like

-1 0.34
0 0.045
1 0.98
1 0.01

My question for this txt file , if u use the vi or gedit, last line is
empty, and by default inserted into to the txt file.

no it's not. the last line ends with a newline. If you're using scanf,
that counts as whitespace.

Try not using scanf - fgets+sscanf will be more robust, and writing your
own parser would probably be even better.
 
M

Mark McIntyre

IF the file is edited by vi or gedit. The result is always false.
However if I use emacs to elimit the last empty line, then the code
work fine.
while(!feof(fp1)){

this is NOT how to use feof. See the FAQ

It may by the way explain your problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,832
Latest member
UtaHetrick

Latest Threads

Top