C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 19th, 2012, 11:36 AM
galatina galatina is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 1 galatina User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 59 sec
Reputation Power: 0
Help! C program not working :)

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!!

Reply With Quote
  #2  
Old November 19th, 2012, 12:12 PM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 54 m 25 sec
Reputation Power: 1774
> while(!feof(SECOND)){
> fscanf(SECOND, "%d %d", &a2, &b2);
First, you should write this as
while ( fscanf(SECOND, "%d %d", &a2, &b2) == 2 )

The other thing to do, since you're reading FIRST repeatedly, is to put in
rewind(FIRST);
before you try to read it.

This will make sure you read FIRST from the start of the file, for every pair of integers read from SECOND.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help! C program not working :)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap