Thread: comparing data in two files

  1. #16
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    If want_names is in the same director as the program you are writing, you don't need the /

    if you are using the /, put the complete path name to the file

    fgets reads one line at a time, google fgets and take the first link for details

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    I think I am getting it ( can't check that out yet, still can't open files).:

    Code:
    while(fgets(topps_file, 4, f1)!=NULL)
    {
    	while(fgets(wants_file, 4, f2)!=NULL)
    
    		if((str=strcmp(topps_file, wants_file))==0)
    
    		fprintf(out, "%s", str)
    }

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I thought you were getting file names from the user using fgets. If not, I'd suggest something like this:
    Code:
    char *secondaryfilenames[] =
    {
        "file1",
        "file2",
        ...the rest of your list...
        NULL,
    };
    
    ...
    
    for( x = 0; secondaryfilenames[ x ]; x++ )
    {
        if( (fp = fopen( secondaryfilenames[ x ], "r" )) == NULL )
        {
            printf( "Failed to open: \'%s\'.\n", secondaryfilenames[ x ] );
        }
        else
        {
            ...read from this file and compare it against your original source file...
        }
    }
    Something along those lines. Assuming of course that you plan to run through a list of files as described earlier.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Forgot to tell, name of files come from commad line.

    that is why I have:

    year=atoi(argv[i]);
    sprintf(fn,"topps.%2d",year); // this name of the file

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You realize that %2d, given a number like "9", will generate "<space>9"? If you want "09" to be generated, use %02d.

    If you're at all suspicious about the filename, print it after you've failed to open it, e.g.
    Code:
    perror(fn);
    [edit] Also, why do you convert it to a number and then right back again to a string? You could just use
    Code:
    strcpy(fn, "fopps.");
    strcat(fn, argv[i]);
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    dwks, I tried, it did not fix the problem, I think the problem is not in a file name, it somewhere else

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I'm not entirely sure what you're trying to do. Here are my guesses.
    • "want.names" (which I hope you've removed the leading slash from) has a list of files that you're trying to open one by one and process somehow.
    • You're also being passed some files on the command line, as numbers, from which your program builds full filenames and processes those as well.
    • Apparently you also have a "master" file, containing lines to be updated; I'm not sure how that's specified.
    • Once you have this information, you open the master file, and open the other files too. Any updated lines in the other files that also occur in the master file (I'm assuming that the lines have some recognizable features and that it's just e.g. a number that changes so you can tell that a line has been updated?) are used to replace the lines in the master file.
    • Then you probably want to write the resulting data out to a new file, perhaps replacing the master file.

    Please, confirm or deny these guesses and then explain in detail what you're trying to do. Actual files that you're dealing with would be helpful too, if they don't contain sensitive data.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Almost
    want.names- master file , contains data for n amount of years,
    1975 year
    156 Smith
    123 Mccarthy and so on , then another year
    This file was created long time ago and never was updated

    12 updated files files which contain only what year it is and numbers but no last names

    I need to create a new file that will look like master file but with updated info, not replace or erase data from master file.


    P.S. (which I hope you've removed the leading slash from) When I am taking slash away, I am not able to open file. ????

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by nynicue View Post
    Almost
    want.names- master file , contains data for n amount of years,
    1975 year
    156 Smith
    123 Mccarthy and so on , then another year
    This file was created long time ago and never was updated

    12 updated files files which contain only what year it is and numbers but no last names

    I need to create a new file that will look like master file but with updated info, not replace or erase data from master file.
    Well, that doesn't sound too hard. Why don't you do this: loop through the lines in the master file. Remember what the last "year" line you saw was. Read all of the names for that year from the master file, and from the corresponding twelve updated files. (I'm assuming these twelve files are one per year or something?) Write out the master's data and the new data to a new file. When you're done, go on to read the next section in the master file (where sections are denoted by "year").

    Something like this:
    Code:
    open master file
    read a line from the master file (assume it's a "year" line)
    repeat until end of file:
        read lines from the file until a "year" line is found:
            write these lines to the output file
        store the "year" line for next iteration (just keep it in the array)
        for each of the updated files which might contain entries for this year:
            get lines from these files
                if a line is a year line, remember the number of the year
                otherwise, if the year matches the year we're currently printing:
                    print this line to the ouput file as well
    I guess the trickiest thing about that idea is that you'll have to stop processing when you see a "year" line, and then use that line for the next iteration.

    P.S. (which I hope you've removed the leading slash from) When I am taking slash away, I am not able to open file. ????
    Well, where is the file? Is it really at the root of your filesystem?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #25
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Well, where is the file? Is it really at the root of your filesystem?
    I have all files copied in the derictories in my Debug folder, I am using visual Studio C++ 2008


    And what if I am open my master file read an year and open corresponding one of the 12 files to that year , then compare all data for that year
    then when I am on a line on next year in master file do the same, and if I do not have corresponding to this year file skip to the next year.
    will it work?


    And thank for your help, Yea, you told it not hard, hmm hope I will say the same in a couple of years.

  11. #26
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And what if I am open my master file read an year and open corresponding one of the 12 files to that year , then compare all data for that year
    then when I am on a line on next year in master file do the same, and if I do not have corresponding to this year file skip to the next year.
    will it work?
    Sounds like a good start . . . try it and see what happens.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to find coomon data in 3 files without rewind..
    By transgalactic2 in forum C Programming
    Replies: 31
    Last Post: 03-29-2009, 03:23 PM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  5. Reading large complicated data files
    By dodzy in forum C Programming
    Replies: 16
    Last Post: 05-17-2006, 04:57 PM