C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 27th, 2004, 11:06 PM
ddeile ddeile is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ddeile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Comparing files one line at a time

Hi,

I am attempting to compare two files one line at a time and then out put a match to a third file.

For example:

file 1:

gsmith 850
hsmith 1520
grock 2568
gmist 45

file 2:

hsmith 1520
grock 2568
gmist 45
gsmith 900


As you can see, the files contain basically the same information (col 1 is a name and col 2 is a cash figure) but will never be in the same order, and are actually much larger than what is shown here.

What I am attempting to do is stream file 1 in one line at a time and then search through file 2 for a matching name and then out put the match as well as the cash figure from both file 1 and file 2 into a seperate output file.

Below is what I have coded so far:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string snam;
string opsnam;
double cash;
double opcash;
double dif;
ifstream stream1("f:/files/c1.txt", ios::in | ios::binary);
ifstream stream2("f:/files/c2.txt", ios::in | ios::binary);
ofstream output("f:/files/outputdata.txt", ios:ut | ios::trunc);

output << "Account Name Cash1 Cash2 Difference" << endl;

while(!stream1.eof()) {
stream1 >> snam >> cash;

while(!stream2.eof()) {

stream2 >> opsnam >> opcash;

if(snam == opsnam){

dif = (cash - opcash);
output << snam << " " << cash << " " << opcash << " " << dif << endl;

}
}
}
return 0;
}


How can I stop stream1 once the name and cash figure on line 1 are read, search stream2 for a matching name and then return to stream1 at the next line to start the process again. Currenty the file stops looping after the first match is found (after it finds a match for the data on line 1 of file 1).

I've tried to do this with both "for" and "while" loops.

Any help is appreciated.

Reply With Quote
  #2  
Old July 28th, 2004, 02:47 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
You need to restart the second file before going into the second loop. Have a look at the function rewind which I think does this, or failing all else just reopen the stream. (Sure someone has a better way than this as I know rewind is c and we are dealing with c++ streams).

If you are using those example files what is happening is the first one gsmith is searched for which is at the end of the second file. The match is found but then the second file always returns eof.

Hope this helps,

-KM-

Reply With Quote
  #3  
Old July 28th, 2004, 10:26 PM
ddeile ddeile is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 ddeile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for your help Kode_monkey. You definately set me in the right direction.

I've solved the problem using the seekg() function in conjuction with the clear() function.

seekg() is setting the second stream back to the begging of the file and the clear() function is removing the eof error created once the stream reaches the end of the file. Here is the code:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string snam;
string opsnam;
double cash;
double opcash;
double dif;
int offset;
offset = 0;
ifstream stream1("f:/files/c1.txt", ios::in | ios::binary);
ifstream stream2("f:/files/c2.txt", ios::in | ios::binary);
ofstream output("f:/files/outputdata.txt", ios:ut | ios::trunc);

output << "Account Name Cash1 Cash2 Difference" << endl;

while(!stream1.eof()) {

stream1 >> snam >> cash;

stream2.seekg(offset, ios::beg);

while(!stream2.eof()) {

stream2 >> opsnam >> opcash;

if(snam == opsnam){

dif = (cash - opcash);
output << snam << " " << cash << " " << opcash << " " << dif << endl;
}


}
stream2.clear();
}

return 0;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Comparing files one line at a time


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT