2 Replies - 190 Views - Last Post: 19 November 2012 - 12:18 PM Rate Topic: -----

#1 galatina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-November 12

Help! Where's the error? :)

Posted 19 November 2012 - 10:48 AM

Hi there guys! I'm trying to build up a program, but it doesn't work... I'm not very expert at C programming, thus I need your help please! :)/>

I've got a first file (let's call it FIRST), which is structured as follows: the first column represents row number (i), the second column represents a datum (a1) and the third column represents another datum (b1).

1 6 7
2 2 3
3 2 5
4 1 6
5 9 9

Then I've a second file (let's call it SECOND) which is structured quite the same: first column represents a datum (a2) and the second one represents another datum (b2).

6 7
1 6
2 1

Basically, I wanna know when the couple (a2,b2) of the SECOND file coincides with one of the couples (a1,b1) of the FIRST file and hence print the row number (i) at which I have this correspondence (in this case it should print the numbers 1 and 4, at which the couples are the same).

Well, I thought of two while cycles, i.e. I open the SECOND file and then for each row I open the FIRST file, looking for couple correspondence and printing the right rows... but it doesn't work, cause it stops at the first row of FIRST file... where's the problem??

while(!feof(SECOND)){

fscanf(SECOND, "%d %d", &a2, &b2);

while(!feof(FIRST)){

fscanf(FIRST, "%d %d %d", &i, &a1, &b1);

if(a1==a2 && b1==b2){printf("%d\n", i);}

}


Thank you in advance! Hope it is clear... sorry for the very stupid question and excuse me for my terrible english, but I'm italian!! :)/>

This post has been edited by jimblumberg: 19 November 2012 - 11:15 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Help! Where's the error? :)

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: Help! Where's the error? :)

Posted 19 November 2012 - 12:14 PM

The FIRST file will be at the end of the file after the inner loop finishes. So I think you need to rewind the file.
Was This Post Helpful? 3
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Help! Where's the error? :)

Posted 19 November 2012 - 12:18 PM

You need to rewind() FIRST once you've read it all.

EDIT: Dammit, should've refreshed!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1