SHARE
TWEET

Course hero

a guest Apr 23rd, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
  2.  
  3. Note: Assuming that the records in file is in below format
  4.  
  5. <PC1 name (string, containing spaces)>
  6.  
  7. <PC1 clock rate (integer)>
  8.  
  9. <PC1 memory (integer)>
  10.  
  11. <PC2 name>
  12.  
  13. ..
  14.  
  15. ..
  16.  
  17. ..
  18.  
  19. //Code
  20.  
  21. #include<stdio.h>
  22.  
  23. #include<string.h>
  24.  
  25. //structure representing a PC
  26.  
  27. typedef struct PC{
  28.  
  29.               char name[20];
  30.  
  31.               int clock_rate;
  32.  
  33.               int memory;
  34.  
  35.               char *description;
  36.  
  37. } PC;
  38.  
  39. //method to read pc info from a file, fill the array, return the number of
  40.  
  41. //records read.
  42.  
  43. //assuming that the file is in below format
  44.  
  45. //<PC name>
  46.  
  47. //<clock speed (integer)>
  48.  
  49. //<memory (integer, in TB)>
  50.  
  51. //...
  52.  
  53. int getpcs(FILE* file, PC array[50]){
  54.  
  55.               int count=0;
  56.  
  57.               char name[20];
  58.  
  59.               int clock;
  60.  
  61.               int memory;
  62.  
  63.               //looping until EOF is encountered, reading a line including spaces
  64.  
  65.               while(fscanf(file,"%[^\n]s\n",name)!=-1){
  66.  
  67.                            //successfully read pc name, so there must be a clock rate and memory
  68.  
  69.                            //reading that also
  70.  
  71.                            fscanf(file,"%d\n",&clock);
  72.  
  73.                            fscanf(file,"%d\n",&memory);
  74.  
  75.                            //creating a PC object, filling the data
  76.  
  77.                            PC pc;
  78.  
  79.                            strcpy(pc.name,name);
  80.  
  81.                            pc.clock_rate=clock;
  82.  
  83.                            pc.memory=memory;
  84.  
  85.                            pc.description=NULL;
  86.  
  87.                            //adding to array, incrementing count
  88.  
  89.                            array[count]=pc;
  90.  
  91.                            count++;
  92.  
  93.               }
  94.  
  95.               //returning count
  96.  
  97.               return count;
  98.  
  99. }
  100.  
  101. int main(){
  102.  
  103.               //code to test the above method.
  104.  
  105.               //creating an array of PC objects
  106.  
  107.               PC array[20];
  108.  
  109.               //getting name of file to read
  110.  
  111.               printf("Enter filename: ");
  112.  
  113.               char filename[20];
  114.  
  115.               scanf("%s",filename);
  116.  
  117.               //opening file for reading
  118.  
  119.               FILE *fp=fopen(filename,"r");
  120.  
  121.               //checking if file opened correctly
  122.  
  123.               if(fp==NULL){
  124.  
  125.                            //error
  126.  
  127.                            printf("file not found\n");
  128.  
  129.                            return 0;
  130.  
  131.               }
  132.  
  133.               //reading data from file
  134.  
  135.               int count=getpcs(fp,array);
  136.  
  137.               printf("Read values:\n");
  138.  
  139.               //displaying all values in the array
  140.  
  141.               for(int i=0;i<count;i++){
  142.  
  143.                            printf("PC: %d\n",i);
  144.  
  145.                            printf("Name: %s\n",array[i].name);
  146.  
  147.                            printf("Clock rate: %d GHz\n",array[i].clock_rate);
  148.  
  149.                            printf("Memory: %d TB\n",array[i].memory);
  150.  
  151.                            printf("Description: %s\n\n",array[i].description);
  152.  
  153.               }
  154.  
  155.               return 0;
  156.  
  157. }
  158.  
  159. /*OUTPUT*/
  160.  
  161. Enter filename: pc.txt
  162.  
  163. Read values:
  164.  
  165. PC: 0
  166.  
  167. Name: Intel core i7
  168.  
  169. Clock rate: 20 GHz
  170.  
  171. Memory: 1 TB
  172.  
  173. Description: (null)
  174.  
  175. PC: 1
  176.  
  177. Name: Intel core i5
  178.  
  179. Clock rate: 3 GHz
  180.  
  181. Memory: 1 TB
  182.  
  183. Description: (null)
  184.  
  185. PC: 2
  186.  
  187. Name: Intel core i7 gen 7
  188.  
  189. Clock rate: 7 GHz
  190.  
  191. Memory: 2 TB
  192.  
  193. Description: (null)
  194.  
  195. PC: 3
  196.  
  197. Name: Intel core i3
  198.  
  199. Clock rate: 2 GHz
  200.  
  201. Memory: 1 TB
  202.  
  203. Description: (null)
  204.  
  205. /*pc.txt file used*/
  206.  
  207. Intel core i7
  208.  
  209. 20
  210.  
  211. 1
  212.  
  213. Intel core i5
  214.  
  215. 3
  216.  
  217. 1
  218.  
  219. Intel core i7 gen 7
  220.  
  221. 7
  222.  
  223. 2
  224.  
  225. Intel core i3
  226.  
  227. 2
  228.  
  229. 1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top