#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h> int main() { FILE* initialText; char** text; int i, countofstr; text = (char**) malloc(100 * sizeof(char*)); for (i = 0; i < 100; i++) { text [i] = (char*) malloc(100 * sizeof(char)); } initialText = fopen("Text.txt", "r"); countofstr = 0; /*ЦИКЛ для подсчета количества строк и записи строк в массив text[n][m]*/ for (i = 0; i < 100; i++) { fgets(text[i], 100, initialText); if (strcmp(text[i], "") == 0) { break; //если строка пустая, т.е. выход из цикла. } countofstr++; } printf("%d\n", countofstr); puts(text[0]); return 0; } There is a formatted text (Text.txt) in which the line does not have additional spaces between the lines. In the text itself can be no more than 100 lines and 100 characters per line. You must enter the text in the two-dimensional array text [n] [m] (n-address of the line; m - address of the character) and count the number of lines in the variable countofstr. For this, a for loop was written, but after its execution in the countofstr variable the wrong value.
Example:
Text.txt:
abcde
abcde
abcde
The result of the program:
18
abcde
text[i][0]==0much more efficient check that the string is empty than strcmp - Mike