Advertisement
Guest User

Input File

a guest
May 28th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1.     int m = 0; // Total of Processes
  2.     int n = 0; // Total of Resources
  3.     int i = 0, j = 0;
  4.     int S[100][100]; // Matrix
  5.  
  6.     FILE *file = fopen("TestCase06.txt", "r");
  7.  
  8.     if(file == NULL)
  9.     {
  10.         printf("The file was not successfully opened.\n");
  11.         exit(1);
  12.     }
  13.     else
  14.     {
  15.         // Count row and column(m and n)
  16.         int stop = 0;
  17.         char ch;
  18.  
  19.         while(!feof(file))
  20.         {
  21.             ch = fgetc(file);
  22.             if(ch == '\n')
  23.             {
  24.                 m++;
  25.                 stop = 1;
  26.             }
  27.             if(stop != 1)
  28.                 if(ch != '\n' && ch != ',')
  29.                     n++;
  30.         }
  31.  
  32.         // Input the matrix
  33.         file = fopen("TestCase06.txt", "r");
  34.         while(!feof(file))
  35.         {
  36.             ch = fgetc(file);
  37.             if(ch != ',' && ch != '\n')
  38.             {
  39.                 S[i][j] = (int)(ch - '0');
  40.             }
  41.  
  42.             if(ch == '\n')
  43.             {
  44.                 i++;
  45.                 j = 0;
  46.             }
  47.             else if(ch != ',')
  48.             {
  49.                 j++;
  50.             }
  51.         }
  52.     }
  53.     fclose(file);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement