Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- Project: 4
- Class: CEG 221
- File Name: Project4p2
- Instructor: DeJongh
- Due Date: May 28th
- Overview: This program ultilizes a menu that allows the user to open binary files with
- data of type STR in them, and to then preform various tasks on the files.
- Program Structure: This program loops a menu for the user to preform various tasks.
- The first option is to simply open a binary file. The second option is
- to count the number of records in the file. The third option is to
- print all the records in the file
- ****************************************************************************/
- #include <stdio.h>
- typedef struct
- {
- int data;
- char name[15];
- }STR;
- void openFile(FILE **fptr); //function prototypes
- int recCount(FILE *fptr);
- void printFile(FILE *fptr);
- void printRec(FILE *fptr);
- void changeRec(FILE *fptr);
- void addRec(FILE *fptr);
- void closeFile(FILE *fptr);
- int main(void)
- {
- int option;
- FILE *fptr = NULL;
- while(option != 8){
- printf("\tMenu\n"); //main menu for the user to select cases
- printf("1. Open File\n");
- printf("2. Count Records\n");
- printf("3. Print Records\n");
- printf("4. Print nth record\n");
- printf("5. Change the nth record\n");
- printf("6. Add a record at the end\n");
- printf("7. Close the file\n");
- printf("8. Exit\n");
- printf("\nInput: ");
- fflush(stdin);
- scanf("%d",&option); //user propmt for menu option
- printf("\n");
- switch (option) { //switch statement that allows user to choose file manipulation
- case 1: openFile(&fptr);
- break;
- case 2: printf("There are %d records in this file.\n",recCount(fptr));
- break;
- case 3: printFile(fptr);
- break;
- case 4: printRec(fptr);
- break;
- case 5: changeRec(fptr);
- break;
- case 6: addRec(fptr);
- break;
- case 7: closeFile(fptr);
- fptr = NULL;
- break;
- case 8: break;
- default: printf("Invalid menu selection.\n");
- }
- system("pause");
- system("cls");
- }//end while
- printf("Thank you for using my program.\n");
- system("pause");
- return 0;
- }//end main
- /*******************************************************************
- This function simply opens a binary file for reading and writing later on.
- Outputs: it does not return anything but sets the file pointer ftpr to the opened file
- ********************************************************************/
- void openFile(FILE **fptr)
- {
- char filename[25];
- do
- {
- printf("Enter file name to open: ");
- scanf("%s",filename);//user propmt to open the desired file
- *fptr = fopen(filename,"r + b");
- if(*fptr == NULL) //tests to see if if file opening was successful or not
- printf("The file was not found.\n");
- }while(*fptr == NULL);
- printf("The file was opened.\n");
- }
- /*******************************************************************
- This function counts the number of records in the file that was read. It does this by
- seeking the file postion marker to the end of the file, taking the number of bytes in the file
- and then dividing it but the size of the data type STR to determine the number of records.
- Outputs: and integer number, which is how many records there are in the file
- ********************************************************************/
- int recCount(FILE *fptr) //counts the number of integers in the file
- {
- int filesize;
- if(fptr == NULL){
- printf("No file is currently open.\n");
- return;
- }
- filesize = fseek(fptr,0,SEEK_END); //set position pointer to end of the file
- filesize = (ftell(fptr) + 1)/sizeof(STR);
- //determine the number of records by dividing the total number of bytes by the size of STR
- return filesize;
- }
- /*******************************************************************
- This function prints all the records in the file that was opened by the openFile function
- Outputs: prints out all of the records in the file, the integer and the string values
- in a list with their corresponding record number
- ********************************************************************/
- void printFile(FILE *fptr)
- {
- int recNum = 0;
- STR record;
- if(fptr == NULL){ //tests to see if a file has been opened
- printf("No file is currently open.\n");
- return;
- }
- fseek(fptr,0,SEEK_SET); //set file position mark to end
- fread(&record, sizeof(STR),1,fptr); //read the first value in the file
- while(!feof(fptr))
- {
- printf("Record %2d: %3d %3s\n",recNum++,record.data,record.name);
- fread(&record,sizeof(STR),1,fptr); //reads the next value in the function with each iteration
- }
- }
- /*******************************************************************
- This function prints the nth record in the file. The record that is printed is input by
- the user.
- Outputs: the nth record in the file which was determined by the user
- ********************************************************************/
- void printRec(FILE *fptr)//prints the desired nth record in the file
- {
- int filesize,nth;
- STR record;
- fpos_t pos;
- if(fptr == NULL){ //checks to see if a file was opened
- printf("No file is currently open.\n");
- return;
- }
- filesize = fseek(fptr,0,SEEK_END); //put position poiner at end of file
- filesize = (ftell(fptr) + 1)/sizeof(STR);
- do
- {
- printf("Please enter the nth Record you would like to print(1...%d): ",filesize);
- scanf("%d",&nth); //user prompt for the nth record
- if(!(nth >0 && nth <= filesize)) //checks to see if the record is valid
- printf("There is no %dth Record.\n",nth);
- }while(!(nth >0 && nth <= filesize));
- pos = (nth - 1) * sizeof(STR); //1 corresponds to the 0 value of the file
- fsetpos(fptr,&pos); //set postion pointer
- fread(&record, sizeof(STR),1,fptr); //read the integer
- printf("The %dth Record is %d %s\n",nth,record.data,record.name);
- }
- /*******************************************************************
- This function changes the nth record which is input by the user. It allows them to
- pick which record they would like to change, and also allows them to change each
- individual part of the record.
- Outputs: it changes the desired file and then tells them that the record was changed
- ********************************************************************/
- void changeRec(FILE *fptr)
- {
- int filesize,nth,newdata;
- STR record;
- fpos_t pos;
- if(fptr == NULL){ //checks to see if a file was opened
- printf("No file is currently open.\n");
- return;
- }
- filesize = fseek(fptr,0,SEEK_END); //put position poiner at end of file
- filesize = (ftell(fptr) + 1)/sizeof(STR);
- do
- {
- printf("Please enter the nth Record you would like to change (1...%d): ",filesize);
- scanf("%d",&nth); //user input for the desired record to change
- if(!(nth >0 && nth <= filesize))
- printf("There is no %dth Record.\n",nth);
- }while(!(nth >0 && nth <= filesize));
- pos = (nth - 1) * sizeof(STR); //1 corresponds to the 0 value of the file
- fsetpos(fptr,&pos); //set postion pointer
- printf("Please enter the new integer data you would like to input: ");
- scanf("%d",&record.data); // user propmt for new int data
- printf("Please enter the new string data you would like to input: ");
- scanf("%s",&record.name);//user prompt for new string data
- fwrite(&record, sizeof(STR), 1, fptr); //writes the new data into the target file
- printf("The new data was written.\n");
- }
- /*******************************************************************
- This function adds are record to the end of the file. It propmts the user for the
- new data and then writes it to the end of the file
- Outputs: adds a new record to the end of the file
- ********************************************************************/
- void addRec(FILE *fptr)
- {
- int filesize,newdata;
- STR record;
- fpos_t pos;
- if(fptr == NULL){
- printf("No file is currently open.\n");
- return;
- }
- filesize = fseek(fptr,0,SEEK_END); //put position poiner at end of file
- filesize = (ftell(fptr) + 1)/sizeof(STR);
- printf("Enter the integer data you would like to input at the end of the file: ");
- scanf("%d",&record.data); //first user prompt for new int data
- printf("Enter the string data you would like to input at the end of the file: ");
- scanf("%s",&record.name);//second user prompt for new string data
- fwrite(&record, sizeof(STR), 1, fptr); //writes the new data to the end of the file
- }
- /*******************************************************************
- This function closes the file
- Outputs: closes the file and then tells the user if it was closed or an error occured
- ********************************************************************/
- void closeFile(FILE *fptr)
- {
- if(fclose(fptr)==0)
- printf("The file was closed.\n");
- else
- printf("An error occured\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement