Updating a binary file

This is a discussion on Updating a binary file within the C Programming forums, part of the General Programming Boards category; I have a small bit of code here, basically getting an error Lvalue required on the line where I have ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Updating a binary file

    I have a small bit of code here, basically getting an error
    Lvalue required
    on the line where I have marked. I think the '+=' symbol is for updating integers and I am updating a string, I could be wrong though, anybody have any ideas?
    Thanks

    Code:
     else {
            printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
            printf("Enter change:");
            scanf("%s",&transaction);
            patient.doctor += transaction ; /*Line where the error shows*/
            printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
            fseek( fPtr, (pat_no -1 ) * sizeof(struct pat_record ), SEEK_SET );
            fwrite( &patient, sizeof( struct pat_record ), 1, fPtr );
            }
        }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would need to see the struct to be 100% sure, but isn't patient.doctor a char array?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    If you want to concatenate two strings, it cannot be done directly with a simple operator,
    use strncat() for instance.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Quote Originally Posted by master5001 View Post
    I would need to see the struct to be 100% sure, but isn't patient.doctor a char array?
    Code:
    struct pat_record{
        int pat_no;
        char name [20];
        int dob;
        char doctor [20];
    }

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by root4 View Post
    If you want to concatenate two strings, it cannot be done directly with a simple operator,
    use strncat() for instance.
    Alright, well I assumed as much. In any event, since all you want to do is concatenate two strings just do as was suggested and use strncat().

    Example:
    Code:
            printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
            printf("Enter change:");
            scanf("%s",&transaction);
            strncat(patient.doctor, transaction, sizeof(patient.doctor));
            printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
            fseek( fPtr, (pat_no -1 ) * sizeof(struct pat_record ), SEEK_SET );
            fwrite( &patient, sizeof( struct pat_record ), 1, fPtr );

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Hi, thanks for the help. tried this but when I run it, the program encounters a windows error and then just ends, it doesnt give any error types just says it has to close?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And what is your program doing when that happens? If we're still on this strncat thing, you might be trying to put more things into patient.doctor than it has room for. The strncat above will write up to 20 characters (the original size of patient.doctor), but there may not be that much room left if patient.doctor already contains something. If you want to overwrite, you should use strncpy, otherwise you'll need to figure out how much room you have left.

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    What tabstop said, and remove the & from the transaction variable in the scanf.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Hi, thanks for the suggestions I wanted to overwrite so I sorted that out, but the error is still happening. The program asks me what patient number to be updated and when I enter the number, the program ends and the error appears.
    Here is my whole function
    Code:
    void binupdaterecord(FILE *fPtr){
        int pat_no;
        char transaction[20];
        struct pat_record patient = { 0 , "" , 0 , "" };
    
        printf("Enter the number of the patient to be updated\n");
        printf(":");
        scanf("%d",&pat_no);
        fseek( fPtr,( pat_no - 1 ) * sizeof( struct pat_record ), SEEK_SET );
        fread( &patient, sizeof( struct pat_record ), 1, fPtr );
    
        if ( patient.pat_no == 0 )
            printf( "Patient number %d has no information.\n", pat_no );
        else {
            printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
            printf("Enter change:");
            scanf("%s",transaction);
            strncpy(patient.doctor, transaction, sizeof(patient.doctor));
            printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
            fseek( fPtr, ( pat_no -1 ) * sizeof( struct pat_record ), SEEK_SET );
            fwrite( &patient, sizeof( struct pat_record ), 1, fPtr );
            }
        }
    Last edited by jaybeeb; 04-23-2008 at 04:04 PM.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am thinking its probably still a buffer overflow problem.

    Try:
    Code:
    scanf("%20s", transaction);
    Check the return value of fseek(). Also, I am positive you are putting your input in correctly, however, you may want to fix it up a bit anyway for anyone else who uses it. i.e.

    Example:
    Code:
    long pat_max = fseek(fPtr, 0, SEEK_END) / sizeof(struct pat_record);
    
    // your code goes here
    scanf("%d", &pat_no);
    if(pat_no > 0 && pat_no < pat_max)
    // more code
    Just a thought.
    Last edited by master5001; 04-23-2008 at 04:13 PM.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Quote Originally Posted by master5001 View Post
    I am thinking its probably still a buffer overflow problem.

    Where is it crashing exactly?
    It asks me to enter the patient number I want to update then once I enter it and press enter it crashes.

    Tried what you said...same thing

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To be brutally honest, if it is crashing before where I thought it would buffer overflow my only thoughts are to add this before you do anything.

    Code:
    if(!fPtr)
    {
       fprintf(stderr, "Invalid file handle.")
       return;
    }

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    fread( &patient, sizeof( struct pat_record ), 1, fPtr );
    I would be sure the read was successful by checking the return value:
    Code:
        if (fread( &patient, sizeof( struct pat_record ), 1, fPtr ) != 1)
        {
            printf("Error reading record &#37;d\n", pat_no);
            return;
        }
    Also be sure you opened the file in binary mode:
    Code:
    if ((fPtr = fopen(filename, "rb+") == NULL)

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Quote Originally Posted by master5001 View Post
    Code:
    long pat_max = fseek(fPtr, 0, SEEK_END) / sizeof(struct pat_record);
    
    // your code goes here
    scanf("%d", &pat_no);
    if(pat_no > 0 && pat_no < pat_max)
    // more code
    This made the program crash when I selected "edit" didnt even get to enter a number?

    Quote Originally Posted by master5001 View Post
    Code:
    if(!fPtr)
    {
       fprintf(stderr, "Invalid file handle.")
       return;
    }
    Not sure exactly where to put this?? (bit of a novice at C)

    Quote Originally Posted by swoopy View Post
    Code:
    >    fread( &patient, sizeof( struct pat_record ), 1, fPtr );
    I would be sure the read was successful by checking the return value:
    Code:
        if (fread( &patient, sizeof( struct pat_record ), 1, fPtr ) != 1)
        {
            printf("Error reading record %d\n", pat_no);
            return;
        }
    Also be sure you opened the file in binary mode:
    Code:
    if ((fPtr = fopen(filename, "rb+") == NULL)
    Put all this in but still getting the same error!?

    Here is my code as it stands now
    Code:
    void binupdaterecord(FILE *fPtr){
        int pat_no;
        char transaction[20];
        struct pat_record patient = { 0 , "" , 0 , "" };
    
    
        if ((fPtr = fopen("binpatient.dat", "rb+") == NULL));
        printf("Enter the number of the patient to be updated\n");
        printf(":");
        scanf("%d",&pat_no);
        fseek( fPtr,( pat_no - 1 ) * sizeof( struct pat_record ), SEEK_SET );
        if(!fPtr)
        {
        fprintf(stderr, "Invalid file handle.");
        return;
        }
        if (fread( &patient, sizeof( struct pat_record ), 1, fPtr ) !=1);
        {
        		printf("Error reading record %d\n", pat_no);
             return;
        }
        if ( patient.pat_no == 0 )
            printf( "Patient number %d has no information.\n", pat_no );
        else {
            printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
            printf("Enter change:");
            scanf("%20s",transaction);
            strncpy(patient.doctor, transaction, sizeof(patient.doctor));
            printf( "%d%s%d%s\n\n", patient.pat_no,patient.name,patient.dob,patient.doctor);
            fseek( fPtr, ( pat_no -1 ) * sizeof( struct pat_record ), SEEK_SET );
            fwrite( &patient, sizeof( struct pat_record ), 1, fPtr );
            }
        }
    Thanks for the help so far!

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    if ((fPtr = fopen("binpatient.dat", "rb+") == NULL));
    if NULL is returned, then the file didn't open, so that would be the place to print an error message and quit:
    Code:
        if ((fPtr = fopen("binpatient.dat", "rb+") == NULL))
        {
            printf("Error opening file binpatient.dat\n");
            return;
        }
    Last edited by swoopy; 04-23-2008 at 05:42 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21