Thread: Merging files (code included)

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Question Merging files (code included)

    Here are the 2 files that I am working with. Attahced is the code that I used to get as close to a solution as I could. The two problems that I have are:
    1) When there is a space in the item name the str stops copying/moving.
    2) For some reason, my code is in some sort of indefinite loop. It does not stop at the end of file, it just keeps printing to the screen.

    FILE 1: FILE 2:

    item code cat price # code

    BASKETBALL 4935 SG 24.95 39 4935
    BIKE 4505 SG 299.99 47 4505
    BLENDER 4240 HW 223.95 69 4240
    CAMERA 9128 SW 26.95 97 9128
    CANVAS 5626 SG 26.95 181 5626
    CARB 9296 AP 31.95 236 9296
    CORN POPPER 6822 HW 29.95 244 6822
    COVER 6115 HW 25.95 265 6115
    CROWBAR 1282 HW 27.95 305 1282
    DARTBOARD 7998 SG 12.95 310 7998
    FUEL GAUGE 3813 SG 151.99 393 3813
    GAS GRILL 3000 AP 149.99 395 3000
    GRIDDLE 3752 HW 39.99 452 3752
    INJECTOR 3116 HW 150.99 474 3116
    IRON 5634 HW 24.95 493 5634
    JACK 1528 HW 13.95 536 1528
    MICROWAVE 6044 HW 30.95 541 6044
    NET 5791 AP 15.95 605 5791
    PRESS 9547 HW 25.95 635 9547
    REFRIG 4111 AP 27.95 745 4111
    STRUTT 3804 AP 152.99 748 3804
    TIRE 2674 AP 32.95 776 2674
    TREADMILL 7348 SG 349.95 780 7348
    VOLLEYBALL 8066 SG 14.95 846 8066
    WASHER 9145 AP 399.99 987 9145

    Both files have the same code (already sorted). I was trying to open both files, compare the code #, and if equal, save this information to a 3rd file.

    Thanks for any help you can provide,

    Alan

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fscanf(report_fp, "%d %s %d %f", &report_code, &str_1, &number_in_stock,
    &total_price_in_stock);

    Before you do this, you need to close the file, and open it for reading

    > 1) When there is a space in the item name the str stops copying/moving.
    Because "%s" stops scanning at the first space.
    I'm assuming that no digits will appear in the description, in which case,
    fscanf(inventry_fp, "%s %d %s %f"
    should be
    fscanf(inventry_fp, "%[^0-9]%d %s %f"

    What this means is treat the first field as consisting of any characters which are not digits.

    This is how you should be reading each line - fscanf is pretty poor
    Code:
        char buff[BUFSIZ];
        while ( fgets( buff, BUFSIZ, inventry_fp) != NULL ) {
            int res = sscanf( buff, "%[^0-9]%d %s %f",
                str_1, &inventry_code,   // you don't need & for arrays
                str_2, &price_per_item );
            if ( res != 4 ) {
                // error - did not convert all the expected values
            }
        }
    > char str_1 [12];
    > char str_2 [2];
    These strings are not large enough.
    Remember strings have a trailing \0 to mark the end of the string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    update attempt at merging

    I have made Salem's suggested changes. I receive 1 error and cannot determine why. It comes on the 2nd char buff[BUFSIZ] line.

    If anyone can help, please let me know.

    Thanks,
    Alan

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > It comes on the 2nd char buff[BUFSIZ] line.
    Because now you're writing C++
    In C, all variables must be declared at the start of the block.

    In this case, the 2nd declaration of buff is unnecessary - just delete it.

    The code should be
    Code:
    while (fgets(buff, BUFSIZ, inventry_fp) != NULL)
    {
        int res = sscanf(buff, "%[^0-9] %d %s %f", str_1, 
            &inventry_code, str_2, &price_per_item);
        if(res != 4)
        {
            printf("Error - did not convert all the expected values!");
        }
    
        // you only want to read one line from this file
        // to match the single line from the other file
        if (fgets(buff, BUFSIZ, instock_fp) != NULL)
        {
            int res = sscanf(buff, "%d %d", &number_in_stock, &instock_code);
            if(res != 2)
            {
                printf("Error - did not convert all the expected values!");
            }
        }
    
        if (inventry_code == instock_code)
        {
            total_price_in_stock = number_in_stock * price_per_item;
            report_code = instock_code;
            fprintf(report_fp, "%d %s %d %f", report_code, str_1, 
                number_in_stock, total_price_in_stock);
        }
        else
            fprintf(stderr, "Inventry Code of: %d does not match instock_code of: %d\n",
                        inventry_code, instock_code);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  2. Urgent: c code to explore zip/war/ear/jar files
    By chinnu in forum C Programming
    Replies: 1
    Last Post: 09-02-2003, 03:09 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  4. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 09:35 AM
  5. Merging Files
    By Natase in forum C Programming
    Replies: 6
    Last Post: 09-25-2001, 07:10 PM