Thread: File Processing using structure and functions

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    File Processing using structure and functions

    Help! I am totally stuck. I need to open a file, then bubble sort it according to manufacturer. Plus I think my program is all screwed up form the functions on down. I must use this setup or I fail the program. Any help with the functions and the sort and search would be great. I have to sort by manufacturer and M.S.R.P. and also do a linear search for them. Thanks!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct
    {
    	char name[80];
    	char type[80];
    	double msrp;
    	double invoice;
    
    } record;
    
    int menu ();
    void decideFile (int);
    void createFile (FILE *);
    void loadFile(FILE * ,struct record );
    void printfFile(FILE *);
    void sortFile(FILE *, struct record);
    void sortManu(record);
    void sortMSRP(record);
    void searchFile(FILE *,struct record);
    void searchManu(record);
    void searchMSRP( record);
    
    
    FILE *filePtr;
    
    int main()
    {
    	
    	int flg=0;
    	int choice;
    
    
    	while(flg==0)
    		choice=menu();
    	if(choice==6)
    		flg=1;
    	else
    		menu(choice);
    
    	return 0;
    }
    int menu ()
    {
    	int menuchoice;
    
    	printf("Enter 1 to create data file: \n");
    	printf("Enter 2 to load data to arrays: \n");
    	printf("Enter 3 to print report: \n");
    	printf("Enter 4 to sort file: \n");
    	printf("Enter 5 to search file: \n");
    	printf("Enter 6 to exit: \n");
    	printf("Enter choice: \n");
    	scanf("%d", &menuchoice);
    
    	return menuchoice;
    }
    void decideFile( int num)
    {
    	int menuchoice;
    	switch (menuchoice)
    	{
    	case 1:
    		createFile(filePtr);
    		break;
    	case 2:
    		loadFile(filePtr,struct record);
    		break;
    	case 3:
    		printFile(filePtr);
    		break;
    	case 4:
    		sortFile(filePtr,struct record);
    		break;
    	case 5:
    		searchFile(filePtr,struct record);
    		break;
    	case 6:
    		exit(1);
    		break;
    	default:
    		printf("Incorrect number! Please enter again!\n");
    		break;
    	}
    
    
    
    }
    void createFile(FILE *filePtr)
    {
    	int flag=TRUE;
    	record car;
    
    	filePtr=fopen("a:\\myfile.dat","w");
    	
    	while(flag)
    	{
    		printf("\nEnter manufacturer (Enter \'END\' when finished): ");
    		scanf("%[^\n]",car.name);
    		if (strcmp(car.name,"END")==0);
    		break;
    		printf("\nEnter type: ");
    		scanf("%s",&car.type);
    		printf("\nEnter invoice:$ ");
    		scanf("%lf",&car.invoice);
    		printf("\nEnter M.S.R.P.: $ ");
    		scanf("%lf",&car.msrp);
    
    		fwrite(&car,sizeof(record),1,filePtr);
    		strset(car.name,' ');
    	}
    	fclose(filePtr);
    
    	return 0;
    }
    void loadFile(FILE *filePtr,struct record auto [])
    {
     record car;
     int index,i;
    
     if ((filePtr=fopen("a:\\myfile.dat","r"))==NULL);
     printf("\nERROR-can't open file!");
     else
     {
    	 index=0;
    	 fread
    
    return 0;
    
    }
    void printFile(FILE *filePtr)
    {
    	int num;
    
          printf("Enter 1 to print file or 2 to exit:\n ");
    	  scanf("%d",&num);
    	  if(num==1)
    
    		
    
    }
    void sortFile(FILE *filePtr,struct record auto[])
    {
     int num;
       
     printf("Enter 1 to sort by manufacturer, 2 to sort by M.S.R.P., or 3 to exit: \n");
     scanf("%d",&num);
    
     if (num==1)
    	 sortManu(FILE *filePtr,record );
     else if (num==2)
    	 sortMSRP(FILE *filePtr, record );
     else
    	 return 0;
    }
    void sortManu(FILE *filePtr,struct record auto[])
    }
    if ((filePtr=fopen("a:\\myfile.dat","r"))==NULL);
     printf("\nERROR-can't open file!");
     else
     {
    	 fscanf(filePtr,"%s",&auto.name);
    
    record car;
    int pass,j,hold;
    
    for(pass=1,pass
    
    
    
    
    }
    void searchFile(FILE *filePtr)
    {
      int num;
       printf("Enter 1 to search by manufacturer, 2 to search by M.S.R.P.: \n");
       scanf("%d", &num);
    
    
    
    }
    Last edited by Randoon; 12-05-2002 at 06:32 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Have a read of this and then edit your post to apply code tags to your code. It'll make it easier for people to help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  2. Help on simple file functions
    By enjoyincubus in forum C Programming
    Replies: 20
    Last Post: 12-01-2006, 04:41 AM
  3. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM