Guest User

Untitled

a guest
May 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAXPOS 100
  5. #define MAXLINES 1000
  6.  
  7. /*
  8.  *      Loads positions file into an array.
  9.  *      returns positions count.
  10.  */
  11. int loadpos(const char *fname, int pos[][2])
  12. {
  13.   FILE *pfile;
  14.   char buffer[128];
  15.   int count = 0;
  16.  
  17.   pfile = fopen(fname, "r");
  18.  
  19.   while(count < MAXPOS && NULL != fgets(buffer, 128, pfile))
  20.   {
  21.         if (sscanf(buffer, "%d:%d", &pos[count][0], &pos[count][1]) == 2)
  22.                 count ++;
  23.   }
  24.  
  25.   fclose(pfile);
  26.   return count;
  27. }
  28.  
  29. /*
  30.  *      Loads book lines into an array.
  31.  *      returns lines count.
  32.  */
  33. int loadbook(const char *fname, char book[][128])
  34. {
  35.   FILE *bfile;
  36.   int count = 0;
  37.  
  38.   bfile = fopen(fname, "r");
  39.  
  40.   while(count < MAXLINES && NULL != fgets(book[count], 128, bfile))
  41.         count++;
  42.  
  43.   fclose(bfile);
  44.   return count;
  45. }
  46.  
  47. void printpos(int count, int pos[][2])
  48. {
  49.   int i;
  50.   for (i = 0; i < count; i++)
  51.         printf("%d:%d\n", pos[i][0], pos[i][1]);
  52. }
  53.  
  54. void printbook(int lines, char book[][128])
  55. {
  56.   int i;
  57.   for (i = 0; i < lines; i++)
  58.         printf("%s", book[i]);
  59. }
  60.  
  61. int main(int argc, char *argv[])
  62. {
  63.   int i, count, lines, pos[MAXPOS][2];
  64.   char book[MAXLINES][128];
  65.   int outofbound = 0;
  66.  
  67.   char output[MAXPOS + 1];
  68.  
  69.   if (argc != 3)
  70.         return 1;
  71.  
  72.   count = loadpos(argv[1], pos);
  73.  
  74.   printpos(count, pos);
  75.  
  76.   lines = loadbook(argv[2], book);
  77.  
  78.   printbook(lines, book);
  79.  
  80.   printf("\nloaded %d lines from book...\n", lines);
  81.  
  82.   for (i = 0; i < count; i++)
  83.   {
  84.         if (pos[i][0] > lines || strlen(book[pos[i][0] - 1]) - 1 < pos[i][1])
  85.         {
  86.                 outofbound = 1;
  87.                 break;
  88.         }
  89.         output[i] = book[pos[i][0] - 1][pos[i][1] - 1];
  90.   }
  91.   output[i] = '\0';
  92.  
  93.   if (outofbound)
  94.         printf("Some positions were out of bound in the text...\nPartially decoded: %s\n", output);
  95.   else
  96.         printf("decoded: %s\n", output);
  97.  
  98.   return 0;
  99. }
Add Comment
Please, Sign In to add comment