fread/fwrite

This is a discussion on fread/fwrite within the C Programming forums, part of the General Programming Boards category; PLEASE DO NOT BE INTIMIDATED BY THE SIZE OF MY CODE, I BELIEVE THE PROBLEM IS VERY SIMPLE AND IT ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    fread/fwrite

    PLEASE DO NOT BE INTIMIDATED BY THE SIZE OF MY CODE, I BELIEVE THE PROBLEM IS VERY SIMPLE AND IT IS MARKED WITH /*ERROR*/. when i run the exe, i can add 2 patients easily to the file. then i try to display them. it displays the first one just fine, but the second it tries to display the first value in the [1]th element of the array, i get an error message. that part of my code is marked with a /*ERROR*/ just before it. easy to find plz help this is due monday. if u can solve my problem then just give me a jpeg and will erect a 500 meter idol of worship in your image.

    heres my code
    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)
    {
    	patientstruct patientstruct1[maxpatients];
    	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","a");
    /**/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;
    	while (!feof(fp))
    	{
    		numread = fread((patientstructpointer)[*count],sizeof(*patientstructpointer),1,fp);
    		fflush(stdin);
    		printf("\npatient's first name\n");
    /*ERROR*/		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);
    		(*count)++;
    	}
    
    		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(;;);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    PLEASE DO NOT BE INTIMIDATED BY THE SIZE OF MY CODE, I BELIEVE THE PROBLEM IS VERY SIMPLE AND IT IS MARKED WITH /*ERROR*/. when i run the exe, i can add 2 patients easily to the file. then i try to display them. it displays the first one just fine, but the second it tries to display the first value in the [1]th element of the array, i get an error message. that part of my code is marked with a /*ERROR*/ just before it. easy to find plz help this is due monday. if u can solve my problem then just give me a jpeg and will erect a 500 meter idol of worship in your image.
    At 405 lines with 4 functions, your code is not particularly long. However, since you consider it long, clearly what you need to do is find the smallest and simplest program that demonstrates your problem.

    Oh, and are you programming in C or C++? You posted this in the C forum, but your code uses C++ constructs and includes a C++ standard library header (well, almost).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    18
    Is that a C or C++ code, I have tried to run it and when I saw the libraries and the cin's I thought there is something wrong in posting this code.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You can start by removing all of those fflush(stdin) calls. A quick google search will tell you over and over that it is a bad thing to do. There go at least a dozen lines of code. Lets see if we can shorten it furthur...

    Your large switch statement with these long lines
    Code:
    ((*patientstructpointer)[*count].locationpatient.subLocation)=((*patientstructpointer)[*count].locationpatient.subLocation)<<1;
    is totally unnecessary. It's far simpler to first use an if-statement to check the range of locationnumber, and then simply shift left by (locationnumber-1). No switch-statement required.
    In the process of removing that switch-statement it should fix one very obvious bug, which again hilights why you should not produce such repetitive code. If you find yourself copying and pasting the same line many times then you're probably doing something wrong.
    That subLocation makes no sense to me anyway though.

    Now about the problem that you know about. Why on earth do you have this bizarre habbit of declaring a variable and also declaring a pointer that you point to that variable, and then only access that variable through the pointer? Why don't you just use the variable? You're not doing dynamic memory allocation, so it isn't some means of preventing your program from blowing up when you read in more items than will fit in the array.

    ALL CAPS IS SHOUTING, you don't need to do that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    3

    i apologise for shouting

    i apologise for shouting, and i also apologise for my round the mulbery bush code, its part of the assignment, not the shouting though.

    yo someone just gave me a hint but they were a little unspecific, he said "it probably didnt clean the buffer between READING the 1st file and inputing the SECOND file"
    so how do i do that? FFLUSH(STDIN);? system("cls");?

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    3
    its also interesting that "numread" returns a zero on the first patient and a 1 on the 2nd patient
    heres my code

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Flushing input buffer:
    http://cpwiki.sourceforge.net/Common...kes_and_errors

    Do NOT use void main. It's undefined.
    http://cpwiki.sourceforge.net/Void_main

    Don't ever use gets or gets_s.
    http://cpwiki.sourceforge.net/Gets

    Don't use "for(;;);" to keep the console from disappearing. Instead see faq on how to prevent the console window from closing.
    Or just run the app with Ctrl+F5.
    Last edited by Elysia; 05-11-2008 at 01:51 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-27-2009, 05:56 PM
  2. fread/fwrite issues
    By elios in forum C Programming
    Replies: 7
    Last Post: 08-17-2003, 06:44 AM
  3. C fread/fwrite problem
    By methodmza in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:44 AM

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