Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define MAXPOS 100
- #define MAXLINES 1000
- /*
- * Loads positions file into an array.
- * returns positions count.
- */
- int loadpos(const char *fname, int pos[][2])
- {
- FILE *pfile;
- char buffer[128];
- int count = 0;
- pfile = fopen(fname, "r");
- while(count < MAXPOS && NULL != fgets(buffer, 128, pfile))
- {
- if (sscanf(buffer, "%d:%d", &pos[count][0], &pos[count][1]) == 2)
- count ++;
- }
- fclose(pfile);
- return count;
- }
- /*
- * Loads book lines into an array.
- * returns lines count.
- */
- int loadbook(const char *fname, char book[][128])
- {
- FILE *bfile;
- int count = 0;
- bfile = fopen(fname, "r");
- while(count < MAXLINES && NULL != fgets(book[count], 128, bfile))
- count++;
- fclose(bfile);
- return count;
- }
- void printpos(int count, int pos[][2])
- {
- int i;
- for (i = 0; i < count; i++)
- printf("%d:%d\n", pos[i][0], pos[i][1]);
- }
- void printbook(int lines, char book[][128])
- {
- int i;
- for (i = 0; i < lines; i++)
- printf("%s", book[i]);
- }
- int main(int argc, char *argv[])
- {
- int i, count, lines, pos[MAXPOS][2];
- char book[MAXLINES][128];
- int outofbound = 0;
- char output[MAXPOS + 1];
- if (argc != 3)
- return 1;
- count = loadpos(argv[1], pos);
- printpos(count, pos);
- lines = loadbook(argv[2], book);
- printbook(lines, book);
- printf("\nloaded %d lines from book...\n", lines);
- for (i = 0; i < count; i++)
- {
- if (pos[i][0] > lines || strlen(book[pos[i][0] - 1]) - 1 < pos[i][1])
- {
- outofbound = 1;
- break;
- }
- output[i] = book[pos[i][0] - 1][pos[i][1] - 1];
- }
- output[i] = '\0';
- if (outofbound)
- printf("Some positions were out of bound in the text...\nPartially decoded: %s\n", output);
- else
- printf("decoded: %s\n", output);
- return 0;
- }
Add Comment
Please, Sign In to add comment