Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <locale.h>
- int string_length(char *s);
- void main()
- {
- setloclale(LC_ALL, "RUSSIAN");
- char s[256];
- char max_string[256];
- int string_counter;
- int max_length;
- int current_length;
- int i;
- int flag_enter; //1 - кончается на \n, 0 - не кончается на \n;
- FILE *in;
- in = fopen("C:\\test\\file.txt", "rt");
- if (in != NULL)
- {
- string_counter = 0;
- max_length = -1;
- while (fgets(s, 256, in) != NULL)
- {
- flag_enter = 0;
- string_counter++;
- current_length = string_length(s);
- if (current_length > max_length)
- {
- max_length = current_length;
- for (i = 0; i <= max_length; i++)
- {
- if (s[i] != '\n') { //символ '\n' запишем как '\0'
- max_string[i] = s[i];
- }
- else {
- max_string[i] = '\0';
- flag_enter = 1;
- }
- }
- }
- else
- {
- for (i = 0; i <= current_length; i++)
- {
- if (s[i] == '\n')
- {
- flag_enter = 1;
- }
- }
- }
- }
- if (feof(in) == 0)
- {
- printf("An error occured.");
- }
- else if ((string_counter == 0))
- {
- printf("This file is empty");
- }
- else
- {
- if (flag_enter == 1) //Если последняя строка пустая
- string_counter++;
- printf("String count = %d\n", string_counter);
- printf("The longest string: \n\"%s\"", max_string);
- printf("\nLength of the longest string = %d\n", max_length);
- }
- fclose(in);
- }
- else
- {
- printf("Cannot open file.");
- }
- }
- int string_length(char *s)
- {
- int i = 0; //Длина строки
- char symb = *(s);
- while ((symb != '\n') && (symb != '\0'))
- {
- i++;
- symb = *(s + i);
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement