comparing 2 txt files

This is a discussion on comparing 2 txt files within the C Programming forums, part of the General Programming Boards category; Hi, i have two text files file-a and file-b and i would like to compare the the text of file-a ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    comparing 2 txt files

    Hi, i have two text files file-a and file-b and i would like to compare the the text of file-a to file-b. the format of file-a is just like a normal essay paragragh and file-b is one word per line. right now what i did was to use fscanf to run through each word in file-a and compare it with file-b. How can i actually pick up a word from file-a and ommit special characters like (,-,+?
    e.g happy+ will jus extract happy instead of happy+... and instead of comparing each word through file-b one by one, is there a better way for me to optimize the speed of the comparison?

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    while (((c=getchar()) && ((c != ',') || (c != '+'))) {

    That way once the word is being read from file a, it will skip the special characters and copy it to b?
    Last edited by JFonseka; 03-13-2008 at 03:18 AM.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I think better still is to say

    while (((c = getchar()) && (((c >= 'A') || (c <= 'Z')) || ((c >= 'a') || ((c <= 'z'))))))

    Not sure about the brackets, check with your editor.

    This way it only gets the alphabet characters, if you need the numerical characters, then you need to add that to the list as well.
    Last edited by JFonseka; 03-13-2008 at 03:28 AM.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    ic ic.. how about any way to speed up the search? will a link link do the job?

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by norhos View Post
    ic ic.. how about any way to speed up the search? will a link link do the job?
    Well what exactly is your algorithm for comparing the words?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by JFonseka View Post
    while (((c = getchar()) && (((c >= 'A') || (c <= 'Z')) || ((c >= 'a') || ((c <= 'z'))))))
    Wow. Horrible code
    I think is along the lines of how you want (made the above code a little more readable):

    Code:
    while
    (
    	( c = getchar() ) != EOF && 
    	(
    		( (c >= 'A') || (c <= 'Z') ) ||
    		( (c >= 'a') || (c <= 'z') )
    	)
    )
    However, I think it might be simplified to this:

    Code:
    while
    (
    	( c = getchar() ) != EOF && isalpha(c)
    )
    Last edited by Elysia; 03-13-2008 at 06:15 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Elysia View Post
    Wow. Horrible code
    I think is along the lines of how you want (made the above code a little more readable):

    Code:
    while
    (
    	( c = getchar() ) != EOF && 
    	(
    		( (c >= 'A') || (c <= 'Z') ) ||
    		( (c >= 'a') || (c <= 'z') )
    	)
    );
    However, I think it might be simplified to this:

    Code:
    while
    (
    	( c = getchar() ) != EOF && isalpha(c)
    );
    I think it's in times like this we say:

    "Thanks Elysia. You're a programming master! How the hell do you know every thing?"

    Now that poor bugger probably implemented what I gave him and got his while loop to not end since no EOF was there and probably still working out the brackets.

    Remind me not to advise anyone, lol.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Oh yeah, and if you want to include numbers, it becomes something like
    Code:
    while
    (
    	( c = getchar() ) != EOF && isalpha(c) && isdigit(c)
    )
    Oh and disregard (remove) the ; at the end, in case you added it. I only added it myself to get the code compiling in my IDE.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    And of course for that you need the ctype library. I hope.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Yep, ctype.h for C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing data in two files
    By nynicue in forum C Programming
    Replies: 25
    Last Post: 06-18-2009, 07:35 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Communication with txt files (continuing)
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 08-13-2007, 08:49 PM
  4. using c++ to edit txt files
    By trescenzi in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2006, 07:54 PM
  5. Comparing Files.
    By dannyp in forum Tech Board
    Replies: 3
    Last Post: 09-12-2004, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21