macro.c #include <stdio.h> #include <stdlib.h> #include <string.h> void main() { char n1, n, cl = 0, i, r = 0; char fn[10][20], ilab[20], iopd[20], m[20][20], oper[20]; FILE *fp1, *fp2, *p[5]; n = 0; fp1 = fopen("main.txt", "r"); if (fp1 == NULL) { perror("Error opening file"); exit(EXIT_FAILURE); } while (!feof(fp1)) { fscanf(fp1, "%s%s%s", ilab, iopd, oper); if (strcmp(iopd, "macro") == 0) { n++; } } printf("No of Macros=%d\n", n); n1 = n; printf("Enter the file name : \n"); for (i = 0; i < n; i++) { scanf("%s", fn[i]); p[i] = fopen(fn[i], "w"); if (p[i] == NULL) { perror("Error creating file"); exit(EXIT_FAILURE); } } n = 0; rewind(fp1); while (!feof(fp1)) { fscanf(fp1, "%s%s%s", ilab, iopd, oper); if (strcmp(iopd, "macro") == 0) { strcpy(m[n], oper); fscanf(fp1, "%s%s%s", ilab, iopd, oper); while (strcmp(iopd, "mend") != 0) { fprintf(p[n], "%s %s %s\n", ilab, iopd, oper); fscanf(fp1, "%s%s%s", ilab, iopd, oper); } fclose(p[n]); n++; } } for (i = 0; i < n; i++) { p[i] = fopen(fn[i], "r"); if (p[i] == NULL) { perror("Error opening file"); exit(EXIT_FAILURE); } fp2 = fopen("outmac.txt", "w"); if (fp2 == NULL) { perror("Error creating file"); exit(EXIT_FAILURE); } rewind(fp1); fscanf(fp1, "%s%s%s", ilab, iopd, oper); } while (!feof(fp1)) { if (strcmp(iopd, "call") == 0) { for (i = 0; i < n; i++) { if (strcmp(m[i], oper) == 0) { rewind(p[i]); fscanf(p[i], "%s%s%s", ilab, iopd, oper); while (!feof(p[i])) { fprintf(fp2, "%s %s %s\n", ilab, iopd, oper); cl = 1; fscanf(p[i], "%s%s%s", ilab, iopd, oper); } break; } } } if (cl != 1) { fprintf(fp2, "%s %s %s\n", ilab, iopd, oper); } cl = 0; fscanf(fp1, "%s%s%s\n", ilab, iopd, oper); } fprintf(fp2, "%s %s %s\n", ilab, iopd, oper); } main.txt ** macro m1 ** move a,b ** mend -- ** macro m2 ** |da b ** mend -- ** start 1000 ** |da a ** call m1 ** call m2 ** add a,b m1.txt ** move a,b m2.txt ** |da b outmac.txt ** macro m1 ** move a,b ** mend -- ** macro m2 ** |da b ** mend -- ** start 1000 ** |da a ** move a,b ** |da b ** add a,b