Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 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.    
  19.     //Defines and open text files
  20.     FILE *input_file, *output_file;
  21.     input_file = fopen("input7_3.txt", "r");
  22.     if(input_file == NULL){
  23.         printf("The input file does not exist or cannot be opened.\n");
  24.         exit(1);
  25.     }
  26.    
  27.     output_file = fopen("output7_3.txt", "w");
  28.     if(input_file == NULL){
  29.         printf("The output file cannot be created or cannot be opened.\n");
  30.         exit(1);
  31.     }
  32.    
  33.     //=============================
  34.    
  35.     //Inroduces the program
  36.     printf("\n");
  37.    
  38.     //=============================
  39.    
  40.     row_count = 0;
  41.     col_count = 0;
  42.     while (fscanf (input_file, "%s", str_array[row_count][col_count]) != EOF) {
  43.         // removes the space after the first word of the line
  44.         c = fgetc (input_file);
  45.         col_count++;
  46.         // read all the remaining words of the current line
  47.         while (c != '\n' && c != EOF) {
  48.             fscanf (input_file, "%s", str_array[row_count][col_count]);
  49.             //printf("%d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
  50.             c = fgetc (input_file);
  51.             //printf("%c\n", c);
  52.             col_count++;
  53.         }
  54.         row_count++;
  55.         col_count = 0;
  56.     } // while
  57.    
  58. //  for(row_count = 0; row_count < 10; row_count++){
  59. //      for(col_count = 0; col_count < 10; col_count++){
  60. //          printf("%s\n", str_array[row_count][col_count]);
  61. //      }
  62. //  }
  63.    
  64.     //======================================
  65.    
  66.     //Closes the files
  67.     fclose(input_file);
  68.     fclose(output_file);
  69.    
  70.     return 0;// Exits the program
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement