Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.73 KB | None | 0 0
  1. /****************************************************************************
  2. Project: 4
  3. Class: CEG 221
  4. File Name: Project4p2
  5. Instructor: DeJongh
  6. Due Date: May 28th
  7. Overview: This program ultilizes a menu that allows the user to open binary files with
  8. data of type STR in them, and to then preform various tasks on the files.
  9. Program Structure: This program loops a menu for the user to preform various tasks.
  10. The first option is to simply open a binary file. The second option is
  11. to count the number of records in the file. The third option is to
  12. print all the records in the file
  13. ****************************************************************************/
  14. #include <stdio.h>
  15.  
  16. typedef struct
  17. {
  18. int data;
  19. char name[15];
  20. }STR;
  21.  
  22. void openFile(FILE **fptr); //function prototypes
  23. int recCount(FILE *fptr);
  24. void printFile(FILE *fptr);
  25. void printRec(FILE *fptr);
  26. void changeRec(FILE *fptr);
  27. void addRec(FILE *fptr);
  28. void closeFile(FILE *fptr);
  29.  
  30.  
  31. int main(void)
  32. {
  33. int option;
  34. FILE *fptr = NULL;
  35.  
  36. while(option != 8){
  37. printf("\tMenu\n"); //main menu for the user to select cases
  38. printf("1. Open File\n");
  39. printf("2. Count Records\n");
  40. printf("3. Print Records\n");
  41. printf("4. Print nth record\n");
  42. printf("5. Change the nth record\n");
  43. printf("6. Add a record at the end\n");
  44. printf("7. Close the file\n");
  45. printf("8. Exit\n");
  46.  
  47. printf("\nInput: ");
  48. fflush(stdin);
  49. scanf("%d",&option); //user propmt for menu option
  50. printf("\n");
  51.  
  52. switch (option) { //switch statement that allows user to choose file manipulation
  53. case 1: openFile(&fptr);
  54. break;
  55. case 2: printf("There are %d records in this file.\n",recCount(fptr));
  56. break;
  57. case 3: printFile(fptr);
  58. break;
  59. case 4: printRec(fptr);
  60. break;
  61. case 5: changeRec(fptr);
  62. break;
  63. case 6: addRec(fptr);
  64. break;
  65. case 7: closeFile(fptr);
  66. fptr = NULL;
  67. break;
  68. case 8: break;
  69. default: printf("Invalid menu selection.\n");
  70. }
  71. system("pause");
  72. system("cls");
  73. }//end while
  74.  
  75.  
  76. printf("Thank you for using my program.\n");
  77. system("pause");
  78. return 0;
  79. }//end main
  80. /*******************************************************************
  81. This function simply opens a binary file for reading and writing later on.
  82.  
  83. Outputs: it does not return anything but sets the file pointer ftpr to the opened file
  84. ********************************************************************/
  85. void openFile(FILE **fptr)
  86. {
  87. char filename[25];
  88.  
  89. do
  90. {
  91. printf("Enter file name to open: ");
  92. scanf("%s",filename);//user propmt to open the desired file
  93.  
  94. *fptr = fopen(filename,"r + b");
  95. if(*fptr == NULL) //tests to see if if file opening was successful or not
  96. printf("The file was not found.\n");
  97. }while(*fptr == NULL);
  98.  
  99. printf("The file was opened.\n");
  100.  
  101. }
  102. /*******************************************************************
  103. This function counts the number of records in the file that was read. It does this by
  104. seeking the file postion marker to the end of the file, taking the number of bytes in the file
  105. and then dividing it but the size of the data type STR to determine the number of records.
  106.  
  107. Outputs: and integer number, which is how many records there are in the file
  108. ********************************************************************/
  109. int recCount(FILE *fptr) //counts the number of integers in the file
  110. {
  111. int filesize;
  112.  
  113. if(fptr == NULL){
  114. printf("No file is currently open.\n");
  115. return;
  116. }
  117.  
  118. filesize = fseek(fptr,0,SEEK_END); //set position pointer to end of the file
  119. filesize = (ftell(fptr) + 1)/sizeof(STR);
  120. //determine the number of records by dividing the total number of bytes by the size of STR
  121. return filesize;
  122. }
  123. /*******************************************************************
  124. This function prints all the records in the file that was opened by the openFile function
  125.  
  126. Outputs: prints out all of the records in the file, the integer and the string values
  127. in a list with their corresponding record number
  128. ********************************************************************/
  129. void printFile(FILE *fptr)
  130. {
  131. int recNum = 0;
  132. STR record;
  133.  
  134. if(fptr == NULL){ //tests to see if a file has been opened
  135. printf("No file is currently open.\n");
  136. return;
  137. }
  138.  
  139. fseek(fptr,0,SEEK_SET); //set file position mark to end
  140. fread(&record, sizeof(STR),1,fptr); //read the first value in the file
  141. while(!feof(fptr))
  142. {
  143. printf("Record %2d: %3d %3s\n",recNum++,record.data,record.name);
  144. fread(&record,sizeof(STR),1,fptr); //reads the next value in the function with each iteration
  145. }
  146. }
  147. /*******************************************************************
  148. This function prints the nth record in the file. The record that is printed is input by
  149. the user.
  150.  
  151. Outputs: the nth record in the file which was determined by the user
  152. ********************************************************************/
  153. void printRec(FILE *fptr)//prints the desired nth record in the file
  154. {
  155. int filesize,nth;
  156. STR record;
  157. fpos_t pos;
  158.  
  159. if(fptr == NULL){ //checks to see if a file was opened
  160. printf("No file is currently open.\n");
  161. return;
  162. }
  163.  
  164. filesize = fseek(fptr,0,SEEK_END); //put position poiner at end of file
  165. filesize = (ftell(fptr) + 1)/sizeof(STR);
  166.  
  167. do
  168. {
  169. printf("Please enter the nth Record you would like to print(1...%d): ",filesize);
  170. scanf("%d",&nth); //user prompt for the nth record
  171. if(!(nth >0 && nth <= filesize)) //checks to see if the record is valid
  172. printf("There is no %dth Record.\n",nth);
  173. }while(!(nth >0 && nth <= filesize));
  174.  
  175. pos = (nth - 1) * sizeof(STR); //1 corresponds to the 0 value of the file
  176.  
  177. fsetpos(fptr,&pos); //set postion pointer
  178. fread(&record, sizeof(STR),1,fptr); //read the integer
  179.  
  180. printf("The %dth Record is %d %s\n",nth,record.data,record.name);
  181. }
  182. /*******************************************************************
  183. This function changes the nth record which is input by the user. It allows them to
  184. pick which record they would like to change, and also allows them to change each
  185. individual part of the record.
  186.  
  187. Outputs: it changes the desired file and then tells them that the record was changed
  188. ********************************************************************/
  189. void changeRec(FILE *fptr)
  190. {
  191. int filesize,nth,newdata;
  192. STR record;
  193. fpos_t pos;
  194.  
  195. if(fptr == NULL){ //checks to see if a file was opened
  196. printf("No file is currently open.\n");
  197. return;
  198. }
  199.  
  200. filesize = fseek(fptr,0,SEEK_END); //put position poiner at end of file
  201. filesize = (ftell(fptr) + 1)/sizeof(STR);
  202. do
  203. {
  204. printf("Please enter the nth Record you would like to change (1...%d): ",filesize);
  205. scanf("%d",&nth); //user input for the desired record to change
  206. if(!(nth >0 && nth <= filesize))
  207. printf("There is no %dth Record.\n",nth);
  208. }while(!(nth >0 && nth <= filesize));
  209.  
  210. pos = (nth - 1) * sizeof(STR); //1 corresponds to the 0 value of the file
  211. fsetpos(fptr,&pos); //set postion pointer
  212.  
  213. printf("Please enter the new integer data you would like to input: ");
  214. scanf("%d",&record.data); // user propmt for new int data
  215.  
  216. printf("Please enter the new string data you would like to input: ");
  217. scanf("%s",&record.name);//user prompt for new string data
  218.  
  219. fwrite(&record, sizeof(STR), 1, fptr); //writes the new data into the target file
  220.  
  221. printf("The new data was written.\n");
  222. }
  223. /*******************************************************************
  224. This function adds are record to the end of the file. It propmts the user for the
  225. new data and then writes it to the end of the file
  226.  
  227. Outputs: adds a new record to the end of the file
  228. ********************************************************************/
  229. void addRec(FILE *fptr)
  230. {
  231. int filesize,newdata;
  232. STR record;
  233. fpos_t pos;
  234.  
  235. if(fptr == NULL){
  236. printf("No file is currently open.\n");
  237. return;
  238. }
  239.  
  240. filesize = fseek(fptr,0,SEEK_END); //put position poiner at end of file
  241. filesize = (ftell(fptr) + 1)/sizeof(STR);
  242.  
  243. printf("Enter the integer data you would like to input at the end of the file: ");
  244. scanf("%d",&record.data); //first user prompt for new int data
  245. printf("Enter the string data you would like to input at the end of the file: ");
  246. scanf("%s",&record.name);//second user prompt for new string data
  247.  
  248. fwrite(&record, sizeof(STR), 1, fptr); //writes the new data to the end of the file
  249.  
  250. }
  251. /*******************************************************************
  252. This function closes the file
  253.  
  254. Outputs: closes the file and then tells the user if it was closed or an error occured
  255. ********************************************************************/
  256. void closeFile(FILE *fptr)
  257. {
  258. if(fclose(fptr)==0)
  259. printf("The file was closed.\n");
  260. else
  261. printf("An error occured\n");
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement