GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Mar-2004, 18:47
Trust Trust is offline
New Member
 
Join Date: Mar 2004
Posts: 3
Trust is on a distinguished road

Comparing Text Files (new programmer)


Hi there! I'm having a bit of trouble with a program I'm trying to write. What I want to do is have two files entered at the command line and then the program will see if they are the same or not (checking character by character). If they are not the same the program will display the letter number where the two files differ. My problem is I'm having difficulty trying to figure out how to write a loop that will read the two files and then compare them character by character. My thoughts were this:

1. Open both text files
2. Read each file into a string buffer
3. Go through the string buffers and compare them to each other character by character until EOF or a different character is present

I know what I want to do but actually writing the code is what is confusing me. Any help would be greatly appriciated. Thanks.
  #2  
Old 27-Mar-2004, 19:20
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello trust.

I think your problem would be better solved by reading the files char by char and comparing them. That way, you don't need to worry about trying to allocate a string buffer and what size it needs to be for the file etc.

I would set a forever loop that would break based upon different conditions. Inside that loop do:
  • Read char from first file(char1=fgetc(fp1))
  • Read char from second file(char2=fgetc(fp2))
  • Check either for eof.
  • If both are eof, set compare to 0 (files are equal) and break
  • If first file is eof, set compare to -1 (file is less than file2) and break
  • If second file is eof, set compare to 1 (file2 is less than file1) and break
  • if char1 is less than char2 set compare to -1 and break
  • if char2 is less than char1 set compare to 1 and break
  • otherwise keep on looping.
  • You could also set a counter that would be incremented to tell what position any difference occured at.

Overall, this isn't too terribly difficult of a program.

Good luck!
d
  #3  
Old 29-Mar-2004, 10:14
Trust Trust is offline
New Member
 
Join Date: Mar 2004
Posts: 3
Trust is on a distinguished road
Thanks dsmith! Your suggestions worked out perfectly.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
burning problems PLEASE PLEASE HELP kelticeire Computer Hardware Forum 4 01-Dec-2006 16:39
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
opening files and displaying text pin215 C++ Forum 7 21-Feb-2004 22:27
Manipulating text Files k209310 C++ Forum 0 17-Nov-2003 11:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.