Thread: Please Help in searching value in text file

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    1

    Please Help in searching value in text file

    Hi all, I'm new here and have quite limited knowledge of C. I'm facing a problem in writing this script. Please give me some hints. Thanks a lot.


    I have a big log file with info like:
    A - 10
    B - 100

    A - 10
    B - 200
    ......

    I have to compare it with another file with info like
    AB - 10100


    I want to compare the to files to see whether the values match and output to another file saying:

    Item 1:
    A - Match
    B - Match
    Item 2:
    A - Match
    B - Mismatch



    How can I do it in C?
    I tried to search for the keywords, but how can I get the values? Is there anything like offset so that I can point to the value of keyword?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Open both files for input
    Open one file for output

    Use two instances of a struct. Each struct will have members like:

    Code:
    struct record {   //define what's in each record
    
       char letr;
       int amount;
    }rec1, rec2;      //and create two instances of this type of struct
    Now read through each file, getting one record of data from file #1, and one record of data from file #2. Your logic can be like:

    Code:
    more1 = fscanf(file_ptr1, "%c, %d", rec1.letr, rec1.amount)
    more2 = same as above but for the other file
    
    while (more1 && more2)  {
    
       make your comparisons rec1.member to rec2.member
    
       write out your data for matches and mismatches, here
    
       and get more data for another loop
       more1 = fscanf( %c, %d, rec1.letr, rec1.amount)
       more2 = same as above but for the other file
       
    }
    when either file is empty, the remaining data in the non-empty file, will 
    all be "mismatched" data, so handle that here, until the file with data, is also empty
    with another while loop 
    
    while(more1)
       //run out all the rest of the data, as "mismatched" to your file
    
    while(more2)
       //same here, for file2 data.
    All this "struct" stuff can be done with just two variables for each file, a char type, and an integer type. Make it clear by their names, which data if from file 1, and which is from file 2. No A and B type variables, make it a1 and a2, and b1 and b2, at least.

    If you have 3 or more things for each file that you want to check and compare, then a struct is the only way that makes sense. With only two things, you could use variables, fine.

    That's how I'd do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a text file for double quotation marks "
    By willie in forum C Programming
    Replies: 4
    Last Post: 11-23-2008, 02:00 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM