Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define NR_ROWS 10
  6. #define NR_COLUMNS 10
  7.  
  8. #define WORD_L  32
  9.  
  10. typedef char WORD [WORD_L];
  11.  
  12. int main(){
  13.    
  14.     //Declares and initializes variables
  15.     int row_count, col_count;
  16.     WORD str_array[NR_ROWS][NR_COLUMNS];
  17.     char c;
  18.     int terminate_flag = 0, pos_m, pos_n, numb_of_lines = 0;
  19.     char temp_str[32];
  20.    
  21.     //Defines and open text files
  22.     FILE *input_file, *output_file;
  23.     input_file = fopen("input7_3.txt", "r");
  24.     if(input_file == NULL){
  25.         printf("The input file does not exist or cannot be opened.\n");
  26.         exit(1);
  27.     }
  28.    
  29.     output_file = fopen("output7_3.txt", "w");
  30.     if(input_file == NULL){
  31.         printf("The output file cannot be created or cannot be opened.\n");
  32.         exit(1);
  33.     }
  34.    
  35.     //=============================
  36.    
  37.     //Inroduces the program
  38.     printf("\n");
  39.    
  40.     //=============================
  41.    
  42.     printf("Note: The counting is starting from 0 like an array.\n");
  43.    
  44.     row_count = 0;
  45.     col_count = 0;
  46.     while (fscanf (input_file, "%s", str_array[row_count][col_count]) != EOF) {
  47.         // removes the space after the first word of the line
  48.         c = fgetc (input_file);
  49.         col_count++;
  50.         // read all the remaining words of the current line
  51.         while (c != '\n' && c != EOF) {
  52.             fscanf (input_file, "%s", str_array[row_count][col_count]);
  53.             printf("%d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
  54.             c = fgetc (input_file);
  55.             printf("%c\n", c);
  56.             col_count++;
  57.         }
  58.         strcpy(str_array[row_count][col_count], "\0");
  59.         printf("T %d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
  60.         //str_array[row_count][col_count]
  61.         numb_of_lines++;
  62.         row_count++;
  63.         col_count = 0;
  64.     } // while
  65.     numb_of_lines--;
  66.    
  67.     printf("%c\n", str_array[4][8]);
  68.    
  69.     for(row_count = 0; row_count < numb_of_lines; row_count++){
  70.         printf("Line %d: ", row_count);
  71.         col_count = 0;
  72.         while(strlen(str_array[row_count][col_count]) > 1){
  73.             printf("%d  ", col_count);
  74.             printf("%s ", str_array[row_count][col_count]);
  75.             col_count++;
  76.         }
  77. //      col_count = 0;
  78.         printf("\n");
  79.     }
  80. // 
  81. //  printf("I - Inserts a new line.\n");
  82. //  printf("D - deletes all lines between m and n\n");
  83. //  printf("R - Replaces 2 lines between m and n\n");
  84. //  printf("E - Terminates editing\n");
  85. // 
  86. //  while(1){
  87. //      printf("Please give an input of I, D, R, or E: ");
  88. //      scanf("%c", &c);
  89. //      fflush(stdin);
  90. //      switch(c){
  91. //          case'i':
  92. //          case'I':{
  93. //              printf("Input the line m that the line will be inserted after: ");
  94. //              scanf("%d", &pos_m);
  95. //              fflush(stdin);
  96. //              row_count = numb_of_lines;
  97. //              while(str_array[row_count][col_count]){
  98. //                 
  99. //              }
  100. //              break;
  101. //          }
  102. //          case'd':
  103. //          case'D':{
  104. //              printf("Input the line m: ");
  105. //              scanf("%d", &pos_m);
  106. //              fflush(stdin);
  107. //              printf("Input the line n: ");
  108. //              scanf("%d", &pos_n);
  109. //              fflush(stdin);
  110. //             
  111. //              break;
  112. //          }
  113. //          case'r':
  114. //          case'R':{
  115. //              printf("Input the line m: ");
  116. //              scanf("%d", &pos_m);
  117. //              fflush(stdin);
  118. //              printf("Input the line n: ");
  119. //              scanf("%d", &pos_n);
  120. //              fflush(stdin);
  121. //             
  122. //              break;
  123. //          }
  124. //          case'e':
  125. //          case'E':{
  126. //              terminate_flag = 1;
  127. //              break;
  128. //          }
  129. //          default: printf("Invalid input.\n");
  130. //      }
  131. //     
  132. //      for(row_count = 0; row_count < numb_of_lines; row_count++){
  133. //          printf("Line %d: ", row_count);
  134. //          for(col_count = 0; col_count < 10; col_count++){
  135. //              printf("%s ", str_array[row_count][col_count]);
  136. //          }
  137. //          printf("\n");
  138. //      }
  139. //     
  140. //      if(terminate_flag == 1){
  141. //          printf("Loop terminated.\n");
  142. //          break;
  143. //      }
  144. //  }
  145.  
  146.    
  147.    
  148.     //======================================
  149.    
  150.     //Closes the files
  151.     fclose(input_file);
  152.     fclose(output_file);
  153.    
  154.     return 0;// Exits the program
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement