Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define NR_ROWS 10
- #define NR_COLUMNS 10
- #define WORD_L 32
- typedef char WORD [WORD_L];
- int main(){
- //Declares and initializes variables
- int row_count, col_count;
- WORD str_array[NR_ROWS][NR_COLUMNS];
- char c;
- int terminate_flag = 0, pos_m, pos_n, numb_of_lines = 0;
- char temp_str[32];
- //Defines and open text files
- FILE *input_file, *output_file;
- input_file = fopen("input7_3.txt", "r");
- if(input_file == NULL){
- printf("The input file does not exist or cannot be opened.\n");
- exit(1);
- }
- output_file = fopen("output7_3.txt", "w");
- if(input_file == NULL){
- printf("The output file cannot be created or cannot be opened.\n");
- exit(1);
- }
- //=============================
- //Inroduces the program
- printf("\n");
- //=============================
- printf("Note: The counting is starting from 0 like an array.\n");
- row_count = 0;
- col_count = 0;
- while (fscanf (input_file, "%s", str_array[row_count][col_count]) != EOF) {
- // removes the space after the first word of the line
- c = fgetc (input_file);
- col_count++;
- // read all the remaining words of the current line
- while (c != '\n' && c != EOF) {
- fscanf (input_file, "%s", str_array[row_count][col_count]);
- printf("%d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
- c = fgetc (input_file);
- printf("%c\n", c);
- col_count++;
- }
- strcpy(str_array[row_count][col_count], "\0");
- printf("T %d, %d: %s\n", row_count, col_count, str_array[row_count][col_count]);
- //str_array[row_count][col_count]
- numb_of_lines++;
- row_count++;
- col_count = 0;
- } // while
- numb_of_lines--;
- printf("%c\n", str_array[4][8]);
- for(row_count = 0; row_count < numb_of_lines; row_count++){
- printf("Line %d: ", row_count);
- col_count = 0;
- while(strlen(str_array[row_count][col_count]) > 1){
- printf("%d ", col_count);
- printf("%s ", str_array[row_count][col_count]);
- col_count++;
- }
- // col_count = 0;
- printf("\n");
- }
- //
- // printf("I - Inserts a new line.\n");
- // printf("D - deletes all lines between m and n\n");
- // printf("R - Replaces 2 lines between m and n\n");
- // printf("E - Terminates editing\n");
- //
- // while(1){
- // printf("Please give an input of I, D, R, or E: ");
- // scanf("%c", &c);
- // fflush(stdin);
- // switch(c){
- // case'i':
- // case'I':{
- // printf("Input the line m that the line will be inserted after: ");
- // scanf("%d", &pos_m);
- // fflush(stdin);
- // row_count = numb_of_lines;
- // while(str_array[row_count][col_count]){
- //
- // }
- // break;
- // }
- // case'd':
- // case'D':{
- // printf("Input the line m: ");
- // scanf("%d", &pos_m);
- // fflush(stdin);
- // printf("Input the line n: ");
- // scanf("%d", &pos_n);
- // fflush(stdin);
- //
- // break;
- // }
- // case'r':
- // case'R':{
- // printf("Input the line m: ");
- // scanf("%d", &pos_m);
- // fflush(stdin);
- // printf("Input the line n: ");
- // scanf("%d", &pos_n);
- // fflush(stdin);
- //
- // break;
- // }
- // case'e':
- // case'E':{
- // terminate_flag = 1;
- // break;
- // }
- // default: printf("Invalid input.\n");
- // }
- //
- // for(row_count = 0; row_count < numb_of_lines; row_count++){
- // printf("Line %d: ", row_count);
- // for(col_count = 0; col_count < 10; col_count++){
- // printf("%s ", str_array[row_count][col_count]);
- // }
- // printf("\n");
- // }
- //
- // if(terminate_flag == 1){
- // printf("Loop terminated.\n");
- // break;
- // }
- // }
- //======================================
- //Closes the files
- fclose(input_file);
- fclose(output_file);
- return 0;// Exits the program
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement