Thread: Compare Files

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    6

    Compare Files

    I want to find if a particular line in File1(ptin) exists in File2(ptout).
    If it aint existing then printf the line File1. I think the code is correct but I am getting blank lines. Please help me with the code.


    Code:
    ptin=fopen("check1.txt","r");
    ptout=fopen(outfile,"r");
    
    if(ptin!=NULL&&ptout!=NULL)
    {
            while(fgets(text, MAX_LEN_SINGLE_LINE, ptin)!=NULL)
            {
    	while(fgets(textchk, MAX_LEN_SINGLE_LINE, ptout)!=NULL)
    	{
    		char *a=strstr(text,textchk);
                                    if(a!=NULL)
                   		    puts(text);
    		else
                                        continue;
    	}//rewind(ptout);
            }
    	 
    }
    else
            printf("File not found :(\n");

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you want to check that the lines match exactly, or just that one is contained in the other?

    Anyway, you're printing the lines that match, as opposed to the ones that don't. And if there are blank lines in the input file, then I guess they'll show up here too.

    In other words: I don't think it's quite clear what you're getting, and how that differs from what you're supposed to be getting?

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    I dono why I am getting blank lines, probably because my logic in the code is wrong. I hope the example below solves your doubts.

    File1.txt
    aabb
    ccdd
    ddee
    ffgg

    File2.txt
    aabbiikk
    ccddlmno
    kkkkkkkk
    jjjjj


    Output expected is:
    ddee
    ffgg

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    fgets doesn't strip out the newline characters, so that's one reason.

    Also, strstr looks for the second argument somewhere inside the first argument.

    Also also, you're printing out everytime there is a match, not if there's no match anywhere in the file.

    ETA: Almost forgot -- you really do need to rewind your file pointer.
    Last edited by tabstop; 07-13-2008 at 08:53 PM.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    So can you give me a code where:
    1. fgets() can be used and newline character is avoided?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ab-c View Post
    So can you give me a code where:
    1. fgets() can be used and newline character is avoided?
    No, because such a thing does not exist. You can, after you read it in with fgets, remove the last character (using something like if (foo[n]=='\n') foo[n]='\0'; where n is chosen appropriately) if it is a newline character.
    Last edited by tabstop; 07-13-2008 at 09:01 PM. Reason: fixed typo

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    6
    So then I use strstr() ?
    Can u embed your previous reply in my code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM