Help - Search - Members - Calendar
Full Version: Fwrite
BleepingComputer.com > Software > Programming
   
chopficaro
im trying to make a database of patients and i am working on writing 2 of 8 of the functions in the menu. displaypatients is supposed to display all the info for the patients 1 by 1, and addpatient is supposed to add a patient to an array of patientstructs in the file. i currently can add one patient, then display him, that works. but if i try to add another patient, the first gets all messed up. i believe the fault lies with my fwrite function wich is marked with a /**/ before it:


CODE
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "process.h"
#define maxpatients 8
using namespace std;

void main();



struct namestruct
{
    char firstName[32];
    char middleI;
    char lastName[32];
};

struct addressstruct
{
    char streetAddress[32];
    char city[32];
    char state[3];
    int zipcode;
};

struct locationstruct
{
    int numFloor;
    int numRoom;
    unsigned char subLocation;
};

struct geninfostruct
{
    addressstruct address;
    char phoneNumber[12];
};

struct insuranceinfostruct
{
    char nameCarrier[32];
    geninfostruct insurancegeninfo;
    float deductable;
};

struct datestruct
{
    int day;
    int month;
    int year;
};

struct patientstruct
{
    namestruct namepatient;
    int age;
    geninfostruct geninfopatient;
    insuranceinfostruct insurence;
    datestruct dischargedate;
    locationstruct locationpatient;
    char information[1024];
};



char menu()
{
    char option1;
    do
    {
    system("cls");
    printf("press a coresponding key and then press enter to choose an option:\n1. open a file\n2. write database to file\n3. display patients\n4. find a patient\n5. add a patient\n6. delete a patient\n7. move a patient\n8.print out patients by discharge date\n");
    option1 = getchar();
    }
    while (option1<'1' || option1 > '7');
    
    fflush(stdin);
    
    return option1;
}

void addpatient(patientstruct (*patientstructpointer)[maxpatients], int (*count)=0)
{
    int numread;
    char yesno;
    FILE *fp;
    fp=fopen("c:\\patientdirectory.txt","r");
    if(fp==NULL)
    {
        printf("file not found. create one?y/n");
        fflush(stdin);
        do
        {
//            scanf_s("%c",&yesno);
            cin>>yesno;
            switch(yesno)
            {    
                case 'y':
                    break;
                case 'n':
                    main();
                    break;
                default:
                    printf("please enter y for yes or n for no and press enter");
                    break;
            }
        }while(yesno!='y');
    }
    else
    {
        while (!feof(fp))
        {
            numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
            (*count)++;
        }
    }


        





    system("cls");

    fflush(stdin);
    printf("\nEnter the patient's first name\n");
    gets_s((*patientstructpointer)[*count].namepatient.firstName);
    fflush(stdin);
    printf("\nEnter the patient's middle initial\n");
    (*patientstructpointer)[*count].namepatient.middleI=getchar();
    fflush(stdin);
    printf("\nEnter the patient's last name\n");
    gets_s((*patientstructpointer)[*count].namepatient.firstName);
    fflush(stdin);
    printf("\nEnter the patient's age\n");
    scanf_s("%d",&(*patientstructpointer)[*count].age);
    fflush(stdin);
    printf("\nEnter the patient's state of residence\n");
    gets_s((*patientstructpointer)[*count].geninfopatient.address.state);
    printf("\nEnter the patient's city of residence\n");
    gets_s((*patientstructpointer)[*count].geninfopatient.address.city);
    fflush(stdin);
    printf("\nEnter the patient's zipcode of residence\n");
    scanf_s("%d",&(*patientstructpointer)[*count].geninfopatient.address.zipcode);
    fflush(stdin);
    printf("\nEnter the patient's street address of residence\n");
    gets_s((*patientstructpointer)[*count].geninfopatient.address.streetAddress);
    fflush(stdin);
    printf("\nEnter the patient's phone number\n");
    gets_s((*patientstructpointer)[*count].geninfopatient.phoneNumber);
    fflush(stdin);
    printf("\nEnter the patient's insurance carrier's name\n");
    gets_s((*patientstructpointer)[*count].insurence.nameCarrier);
    fflush(stdin);
    printf("\nEnter the patient's insurance carrier's state\n");
    gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.state);
    fflush(stdin);
    printf("\nEnter the patient's insurance carrier's city\n");
    gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.city);
    fflush(stdin);
    printf("\nEnter the patient's insurance carrier's zipcode\n");
    scanf_s("%d",&(*patientstructpointer)[*count].insurence.insurancegeninfo.address.zipcode);
    fflush(stdin);
    printf("\nEnter the patient's insurance carrier's street address\n");
    gets_s((*patientstructpointer)[*count].insurence.insurancegeninfo.address.streetAddress);
    fflush(stdin);
    printf("\nEnter the patient's insurance carrier's deductable\n");
    scanf_s("%f",&(*patientstructpointer)[*count].insurence.deductable);
    fflush(stdin);
    printf("\nEnter the patient's year of discharge\n");
    scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.year);
    fflush(stdin);
    printf("\nEnter the patient's month of discharge\n");
    scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.month);
    fflush(stdin);
    printf("\nEnter the patient's day of discharge\n");
    scanf_s("%d",&(*patientstructpointer)[*count].dischargedate.day);
    fflush(stdin);
    printf("\nEnter the patient's floor number\n");
    scanf_s("%d",&(*patientstructpointer)[*count].locationpatient.numFloor);
    fflush(stdin);
    printf("\nEnter the patient's room number\n");
    scanf_s("%d",&(*patientstructpointer)[*count].locationpatient.numRoom);
    fflush(stdin);
    printf("\nEnter the patient's location number\n");
//location
    ((*patientstructpointer)[*count].locationpatient.subLocation)=1;
    int locationnumber;
    scanf_s("%d",&locationnumber);
        switch(locationnumber)
        {    
        case 1:
            break;
        case '2':
            ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<1;
            break;
        case '3':
            ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<2;
            break;
        case '4':
            ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<3;
            break;
        case '5':
            ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<4;
            break;
        case '6':
            ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<5;
            break;
        case '7':
            ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<6;
            break;
        case '8':
            ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<7;
            break;
        default:
            printf("you're fired");
        }
    fflush(stdin);
    printf("\nEnter the patient's description (less than 1023 characters please)\n");
    gets_s((*patientstructpointer)[*count].information);
    fp=fopen("c:\\patientdirectory.txt","w");
/**/fwrite ((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
}





void displaypatients()
{
    FILE *fp;
    fp=fopen("c:\\patientdirectory.txt","r");
    patientstruct (*patientstructpointer)[maxpatients];
    patientstruct (patientstruct1)[maxpatients];
    (patientstructpointer)=&(patientstruct1);
    int numread;
    int *count;
    int asa;
    count=&asa;
    asa=0;
    numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
    fflush(stdin);
    printf("\npatient's first name\n");
    puts((*patientstructpointer)[*count].namepatient.firstName);
    fflush(stdin);
    printf("\npatient's middle initial\n");
    printf("%c",(*patientstructpointer)[*count].namepatient.middleI);
    fflush(stdin);
    printf("\npatient's last name\n");
    puts((*patientstructpointer)[*count].namepatient.firstName);
    fflush(stdin);
    printf("\npatient's age\n");
    printf("%d",(*patientstructpointer)[*count].age);
    fflush(stdin);
    printf("\npatient's state of residence\n");
    puts((*patientstructpointer)[*count].geninfopatient.address.state);
    printf("\npatient's city of residence\n");
    puts((*patientstructpointer)[*count].geninfopatient.address.city);
    fflush(stdin);
    printf("\npatient's zipcode of residence\n");
    printf("%d",(*patientstructpointer)[*count].geninfopatient.address.zipcode);
    fflush(stdin);
    printf("\npatient's street address of residence\n");
    puts((*patientstructpointer)[*count].geninfopatient.address.streetAddress);
    fflush(stdin);
    printf("\npatient's phone number\n");
    puts((*patientstructpointer)[*count].geninfopatient.phoneNumber);
    fflush(stdin);
    printf("\npatient's insurance carrier's name\n");
    puts((*patientstructpointer)[*count].insurence.nameCarrier);
    fflush(stdin);
    printf("\npatient's insurance carrier's state\n");
    puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.state);
    fflush(stdin);
    printf("\npatient's insurance carrier's city\n");
    puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.city);
    fflush(stdin);
    printf("\npatient's insurance carrier's zipcode\n");
    printf("%d",(*patientstructpointer)[*count].insurence.insurancegeninfo.address.zipcode);
    fflush(stdin);
    printf("\npatient's insurance carrier's street address\n");
    puts((*patientstructpointer)[*count].insurence.insurancegeninfo.address.streetAddress);
    fflush(stdin);
    printf("\npatient's insurance carrier's deductable\n");
    printf("%f",(*patientstructpointer)[*count].insurence.deductable);
    fflush(stdin);
    printf("\npatient's year of discharge\n");
    printf("%d",(*patientstructpointer)[*count].dischargedate.year);
    fflush(stdin);
    printf("\npatient's month of discharge\n");
    printf("%d",(*patientstructpointer)[*count].dischargedate.month);
    fflush(stdin);
    printf("\npatient's day of discharge\n");
    printf("%d",(*patientstructpointer)[*count].dischargedate.day);
    fflush(stdin);
    printf("\npatient's floor number\n");
    printf("%d",(*patientstructpointer)[*count].locationpatient.numFloor);
    fflush(stdin);
    printf("\npatient's room number\n");
    printf("%d",(*patientstructpointer)[*count].locationpatient.numRoom);
    fflush(stdin);
    printf("\npatient's location number\n");
//location
    int locationnumber;
        switch((*patientstructpointer)[*count].locationpatient.subLocation)
        {    
        case 1:
            locationnumber=1;
            break;
        case (1<<1):
            locationnumber=2;
            break;
        case (1<<2):
            locationnumber=3;
            break;
        case (1<<3):
            locationnumber=4;
            break;
        case (1<<4):
            locationnumber=5;
            break;
        case (1<<5):
            locationnumber=6;
            break;
        case (1<<6):
            locationnumber=7;
            break;
        case (1<<7):
            locationnumber=8;
            break;
        default:
            locationnumber=0;
            printf("you're fired");
        }
        printf("%d",locationnumber);
    fflush(stdin);
    printf("\npatient's description (less than 1023 characters please)\n");
    puts((*patientstructpointer)[*count].information);
    printf("\npress any key to continue\n");
    getchar();
}







void main()
{
    patientstruct patientstruct1[maxpatients];
    char entry;
    int count = 0;
    do
    {
        entry = menu();
        switch(entry)
        {        
        case '1':

            break;
        case '2':

            break;
        case '3':
            displaypatients();
            break;
        case '4':

            break;
        case '5':
            addpatient(&patientstruct1,&count);
            break;
        case '6':

            break;
        case '7':

            break;
        case '8':

            break;
        default:
            printf("you're fired");
        }
    }
    while (entry != '7');    






for(;;);
}
groovicus
Instead of making us page through 500 lines of code, is there any chance you could just show the part that you think is causing a problem?
chopficaro
go edit --> find --> /**/
that will bring you to the part that i think is the problem
Billy O'Neal
I tried to compile your application and it does not work. The menu at the beginning does not allow me to reproduce what you are trying to look at.

Your functions are writing to all kinds of undefined memmory! (See below)

QUOTE
void main();

You do not have to prototype MAIN()

Also, MAIN should not return a void value. It should return INT, and the value to the operating system should be EXIT_SUCCESS or EXIT_FAILURE.

groovicus, this is the line in question:
CODE
fwrite ((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);


see if making it this works:
CODE
fseek(fp,0,SEEK_END);
fwrite ((patientstructpointer+*count),sizeof(*patientstructpointer),1,fp);


You should also note that C will pad the actual size of the structure to suit it's needs. If you are relying on each use of this program to output a fixed sized structure each use, that may not be the case. Also

QUOTE
fp=fopen("c:\\patientdirectory.txt","r");

Why are you only opening the file for read access if you are going to write to it?

QUOTE
fflush(stdin);

Is there any particular reason you keep doing this?

QUOTE
(*patientstructpointer)[*count]

Why are you referencing Count as a pointer? If it's for pass by refrence, you cannot do this:
int (*count)=0
as an argument
and expect count to be zero by default. In this case, you are telling the compiler that if no pointer is passed, use a null pointer. What happens when you use a null pointer? There's absolutely no reason for the default argument here.

QUOTE
// scanf_s("%c",&yesno);
cin>>yesno;

Use the ScanF function here. The cin statement is a buffer overrun hole. Nothing is preventing me from trying to write 10 chars into that char memory location. And that's bad.

In the file save function, you are calling main();. While this is technically allowed by C89, (and therefore c++) doing so can be a problem because when your program ends, main returns inside of your function, and the program will contunue after that point. Calling main does not restart the program. Rather, it calls main just like it would call any other function. Therefore, when main returns, it will return to that point in the addpatient() function, not to the operating system.

QUOTE
void addpatient(patientstruct (*patientstructpointer)[maxpatients]

Do you want an array of pointers, or a pointer to an array? If its a pointer to an array, you want
void addpatient(patientstruct *patientstructpointer

Here is the finished product:
Call it with this (in main):
CODE
while (!(addpatient(patientstruct1,&count))); //repeat until sucess


and the function:
CODE
int addpatient(patientstruct *patientstructpointer, int *count)
{
    //now returns 0 on sucess, 1 on failure
    int numread = 0;
    char yesno;
    FILE *fp;
    fp=fopen("c:\\patientdirectory.txt","r");
    if(fp==NULL)
    {
        printf("file not found. create one?y/n");
        do
        {
            scanf("%c",&yesno);
            switch(yesno)
            {    
                case 'y':
                    fclose(fp);
                    fp = fopen("c:\\patientdirectory.txt","w");
                    break;
                case 'n':
                    return 1; //failure
                    break;
                default:
                    printf("please enter y for yes or n for no and press enter");
                    break;
            }
        }while(yesno!='y');
    }
    else
    {
        fclose(fp);
        fp = fopen("c:\\patientdirectory.txt","r+");
        while (!feof(fp))
        {
            numread++;
            fread((patientstructpointer+numread),sizeof(*patientstructpointer),1,fp);
        }
    }
    system("cls");
    numread++; //increment one past the number read
    printf("\nEnter the patient's first name\n");
    gets_s((*(patientstructpointer+numread)).namepatient.firstName);
    printf("\nEnter the patient's middle initial\n");
    (*(patientstructpointer+numread)).namepatient.middleI=getchar();
    printf("\nEnter the patient's last name\n");
    gets_s((*(patientstructpointer+numread)).namepatient.firstName);
    printf("\nEnter the patient's age\n");
    scanf_s("%d",&(*(patientstructpointer+numread)).age);
    printf("\nEnter the patient's state of residence\n");
    gets_s((*(patientstructpointer+numread)).geninfopatient.address.state);
    printf("\nEnter the patient's city of residence\n");
    gets_s((*(patientstructpointer+numread)).geninfopatient.address.city);
    printf("\nEnter the patient's zipcode of residence\n");
    scanf_s("%d",&(*(patientstructpointer+numread)).geninfopatient.address.zipcode);
    printf("\nEnter the patient's street address of residence\n");
    gets_s((*(patientstructpointer+numread)).geninfopatient.address.streetAddress);
    printf("\nEnter the patient's phone number\n");
    gets_s((*(patientstructpointer+numread)).geninfopatient.phoneNumber);
    printf("\nEnter the patient's insurance carrier's name\n");
    gets_s((*(patientstructpointer+numread)).insurence.nameCarrier);
    printf("\nEnter the patient's insurance carrier's state\n");
    gets_s((*(patientstructpointer+numread)).insurence.insurancegeninfo.address.state);
    printf("\nEnter the patient's insurance carrier's city\n");
    gets_s((*(patientstructpointer+numread)).insurence.insurancegeninfo.address.city);
    printf("\nEnter the patient's insurance carrier's zipcode\n");
    scanf_s("%d",&(*(patientstructpointer+numread)).insurence.insurancegeninfo.address.zipcode);
    printf("\nEnter the patient's insurance carrier's street address\n");
    gets_s((*(patientstructpointer+numread)).insurence.insurancegeninfo.address.streetAddress);
    printf("\nEnter the patient's insurance carrier's deductable\n");
    scanf_s("%f",&(*(patientstructpointer+numread)).insurence.deductable);
    printf("\nEnter the patient's year of discharge\n");
    scanf_s("%d",&(*(patientstructpointer+numread)).dischargedate.year);
    printf("\nEnter the patient's month of discharge\n");
    scanf_s("%d",&(*(patientstructpointer+numread)).dischargedate.month);
    printf("\nEnter the patient's day of discharge\n");
    scanf_s("%d",&(*(patientstructpointer+numread)).dischargedate.day);
    printf("\nEnter the patient's floor number\n");
    scanf_s("%d",&(*(patientstructpointer+numread)).locationpatient.numFloor);
    printf("\nEnter the patient's room number\n");
    scanf_s("%d",&(*(patientstructpointer+numread)).locationpatient.numRoom);
//location
    int locationnumber;
    do
    {
        scanf_s("%d",&locationnumber);
        if (locationnumber <= 8 && locationnumber >= 1)
        {
            (*(patientstructpointer+numread)).locationpatient.subLocation = locationnumber;
        }
        else
        {
            printf("you're fired; try again");
        }
    } while (!(locationnumber <= 8 && locationnumber >= 1));
    printf("\nEnter the patient's description\n");
    gets_s((*(patientstructpointer+numread)).information,1022);
    fp=fopen("c:\\patientdirectory.txt","w");
    scanf_s("%c",&yesno);
    fwrite ((patientstructpointer+numread),sizeof(*patientstructpointer),1,fp);
*count = numread;
    return 0; //sucess
}


Hope that helps,
Billy3
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.