Advertisement
Guest User

Untitled

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