Thread: Search and edit structure in binary file

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    12

    Search and edit structure in binary file

    Hello,

    I am struggling with my code. I have a structure in a binary file and I need to make a function that searches a structure FIRM by a number and then proceeds to edit its previously typed profit.

    This is the code in the main function:
    Code:
    case 4:        {
                fflush(stdin);
                printf("\nUpdate profit for firm with ID number:\n");
                gets(searchM);
                printf("\nNew profit:\n");
                scanf("%d", &newprofit);
                if (updateprofit(binary, filename, searchM, newprofit))
                    printf("Update done!\n");
                else
                    printf("Not updated!\n");
                system("pause");
                break;
            }
    This is the header:
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <malloc.h>
    #include <string.h>
    
    
    struct firm
    {
        char idn[13];
        char name[40];
        char date[10];
        int profit;
        
    };
    typedef struct firm FIRM;
    
    
    FILE *writeFile(char *fname);
    void enter(FIRM **ps);
    int readFile(FILE *fp, char *fname);
    void display(FIRM *ps);
    FILE *addInfo(char *fname);
    int updateprofit(FILE *fp, char *fname, char *searchM, int newprofit);
    And this is the function itself that gives and error and breaks the whole program.

    Code:
    int updateprofit(FILE *fp, char *fname, char *searchM, int newprofit){
        FIRM s;
        fp = fopen(fname, "r+b");
        if (!fp)
            return 0;
        while (fread(&s, sizeof(FIRM), 1, fp) == 1)
        {
            if (strcmp(s.profit, searchM) == 0)
            {
                s.profit = newprofit;
                fseek(fp, ftell(fp) - sizeof(FIRM), SEEK_SET);
                fwrite(&s, sizeof(FIRM), 1, fp);
                fclose(fp);
                return 1;
            }
        }
        fclose(fp);
        return (0);
    }
    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the error, i.e., how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    12
    Search and edit structure in binary file-capture-jpg

    After I hit enter on new profit this happens.

  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
    Don't use gets()
    Don't use fflush(stdin)

    malloc.h is an obsolete file, malloc is prototyped in stdlib.h

    > typedef struct firm FIRM;
    The upper case namespace is usually reserved for #defines

    > if (strcmp(s.profit, searchM) == 0)
    profit is an integer, and searchM is a string.
    How are you NOT getting any compiler warnings for this?
    If you're getting warnings and ignoring them, you're a fool.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    12
    I get exactly 0 warnings

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by ghost00359 View Post
    I get exactly 0 warnings
    Then you need to increase the warning level of your compiler.
    I assume you understand what the problem is as Salem showed you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search in binary file
    By krakatao in forum C Programming
    Replies: 6
    Last Post: 02-11-2012, 06:38 AM
  2. Replies: 4
    Last Post: 10-19-2009, 12:10 AM
  3. Hex edit binary file
    By andwan0 in forum C Programming
    Replies: 4
    Last Post: 09-03-2009, 09:30 AM
  4. How to search within a binary file
    By khpuce in forum C Programming
    Replies: 17
    Last Post: 04-12-2005, 05:23 AM
  5. Binary search on a HUGE file.
    By Appoloinus in forum C++ Programming
    Replies: 12
    Last Post: 03-03-2003, 12:29 AM

Tags for this Thread