By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,952 Members | 1,119 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,952 IT Pros & Developers. It's quick & easy.

my question about read file !

P: n/a
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;
}

Dec 5 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2005-12-05, yezi <ye*****@hotmail.com> wrote:
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.
Dec 5 '05 #2

P: n/a
On 5 Dec 2005 15:11:53 -0800, in comp.lang.c , "yezi"
<ye*****@hotmail.com> wrote:
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.


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 5 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.