Advertisement
Tprind

C code

Nov 3rd, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define RobMax 10
  6.  
  7. typedef struct {
  8. int Robot_Number;
  9. char Robot_Name[20];
  10. int Year_Manufactured;
  11. float Top_Speed;
  12. int Strength;
  13. int Mass;
  14. float Best_Score;
  15. }robot_t;
  16.  
  17. int main(){
  18. int targetNumber;
  19.  
  20. //create an array of struct,
  21. robot_t robot_info[10] =
  22. {{1, "Bender", 2054, 30.5, 80, 60, 0},
  23. {2, "Crusher", 1979, 15, 86, 80, 0},
  24. {3, "Sprinter", 2042, 45, 41, 30, 0},
  25. {4, "Vulc4n", 2178, 47, 90, 50, 0}};
  26.  
  27.  
  28. //opening a file for reading and writing
  29. FILE* file =fopen("Robot.txt", "r+w");
  30. if(file ==NULL){
  31. printf("ERROR: file failed to open!\n");
  32. exit(-1);
  33. }
  34.  
  35. /*//Initialising the .txt file
  36. for (i=0; i<4; i++){
  37. fprintf(file, "%d %s %d %f %d %d %f\n",
  38. robot_info[i].Robot_Number,
  39. robot_info[i].Robot_Name,
  40. robot_info[i].Year_Manufactured,
  41. robot_info[i].Top_Speed,
  42. robot_info[i].Strength,
  43. robot_info[i].Mass,
  44. robot_info[i].Best_Score);
  45.  
  46. }*/
  47.  
  48. printf("you selected search a robot by number. (Type 0 to view all avalible robots)\n");
  49. scanf("%d", &targetNumber);
  50.  
  51. int RNo, YM, S, M;
  52. char RNa[20];
  53. float TS, BS;
  54.  
  55. if (targetNumber != 0){
  56. printf("Robot ID: %d's information:\n", targetNumber);
  57. while( fscanf(file, "%d%19s%d%f%d%d%f", &RNo, RNa, &YM, &TS, &S, &M, &BS) ==1) {
  58. robot_info[targetNumber].Robot_Number = RNo;
  59. strcpy(robot_info[targetNumber].Robot_Name, RNa);
  60. robot_info[targetNumber].Year_Manufactured = YM;
  61. robot_info[targetNumber].Top_Speed = TS;
  62. robot_info[targetNumber].Strength = S;
  63. robot_info[targetNumber].Mass = M;
  64. robot_info[targetNumber].Best_Score = BS;
  65. printf("Num: %d, Name: %19s, Manufactured: %d, Top Speed: %f, Strength: %d, Mass: %d, Best Score: %f.", RNo, RNa, YM, TS, S, M, BS);
  66. }
  67. }
  68. fclose(file);
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement