Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Need Some Help with File Handling

$
0
0
Our professor gave this problem to us
Machine Problem 1 – Meet My MASTER

Write a program to create a sequential master file that contains data about an inventory of parts in which the key field is a part number.


INPUT
- part number
- part description
- part price

OUTPUT
- Message indicating that the transaction was successfully finished.
- MASTER.DAT that contains the recorded information.

SAMPLE RUN

Part Number: ASUS101112
Part Description: ASUS 101 Motherboard
Part Price: 4500.00

Another (Y/N)?: Y


Part Number: BOKA118001
Part Description: BOKA 118 Speakers
Part Price: 3500.00

Another (Y/N)?: N

Transactions saved. Press any key to continue…


NOTE: The sequential master file must be sorted and must be saved as a text file, meaning it can be viewed using any text editor. You could design your own input screen.

and I came up with this code
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>



struct record{
	char partnum[50];
	char partdesc[50];
	double partprice;
	}master,c;

	FILE *data,*temp,*temp2;
	
void input();
void filecheck();
void sort();

int i=0;
	
int main()
{	char ans;
	

	do{	
		
		system("cls");
		system("COLOR 2");
		input();
		printf("\nAnother? [y/n]: ");
		ans=getche();
		}while(toupper(ans)=='Y');	    
	    printf("\nTransactions saved. Press any key to continue...");
	    
getch();	
}

void input()
{	double price;
		printf("\n");
		printf("Input:\n");
		printf("\tPart Number: ");
		scanf("\n");
		gets(master.partnum);
		printf("\n\tPart Description: ");
		gets(master.partdesc);
		printf("\n\tPart Price: ");
		scanf("%lf",&price);
		printf("\n");
		master.partprice=price;	
		for(int i=0;i<strlen(master.partdesc);i++)
        if(master.partdesc[i]==' ')
		master.partdesc[i]='_';
		filecheck();
}

void filecheck()
{
	data=fopen("master.dat","r");
		if(data==NULL)
		{fclose(data);
		data=fopen("master.dat","w");
		fprintf(data,"%s\t\t%s\t\t%.2lf\n",master.partnum,
master.partdesc,master.partprice);
		fclose(data);
		}
		else {sort();}
	
}
void sort()
{	
	fclose(data);
	data=fopen("master.dat","r");
	temp=fopen("master2.dat","a");
	while(fscanf(data,"%s\t\t%s\t\t%lf\n",c.partnum,c.
partdesc,&c.partprice)!=EOF)
 	if(strcmpi(c.partnum,master.partnum)<0)
		 {fprintf(temp,"%s\t\t%s\t\t%.2lf\n",c.partnum,c.pa
rtdesc,c.partprice);
		  }
		else if(strcmpi(c.partnum,master.partnum)==0)
		{printf("\nFile already Exist!");break;}
		else
		  {break;}
		fprintf(temp,"%s\t\t%s\t\t%.2lf\n",master.partnum,
master.partdesc,master.partprice);
		if(i<=0)
		{fprintf(temp,"%s\t\t%s\t\t%.2lf\n",c.partnum,c.pa
rtdesc,c.partprice);}
		i++;
	fclose(temp);
	fclose(data);
	remove("master.dat");
	rename("master2.dat","master.dat");
    fflush(temp);
}


The problem is it does not save everything that has been on the INPUT. Can somebody help me please? Thanks alot! :)


Viewing all articles
Browse latest Browse all 2703

Trending Articles