52 lines
972 B
C++
52 lines
972 B
C++
#include <iostream>
|
|
#include <cstring>
|
|
|
|
#define SIZE_LINE 1000+1
|
|
|
|
using namespace std;
|
|
|
|
char * shorterString(FILE * file, char * const strmin);
|
|
|
|
int main(void) {
|
|
FILE * file = fopen("parole.txt", "rt");
|
|
|
|
if (file != NULL) {
|
|
char strmin[SIZE_LINE] = "";
|
|
|
|
shorterString(file, strmin);
|
|
|
|
if (strlen(strmin) > 0) {
|
|
cout << "Shortest line: " << strmin << endl;
|
|
} else {
|
|
cout << "The file or line is empty or invalid." << endl;
|
|
}
|
|
|
|
fclose(file);
|
|
} else {
|
|
perror("Error (source)");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
char * shorterString(FILE * file, char * const strmin) {
|
|
char str[SIZE_LINE];
|
|
int minLength = SIZE_LINE;
|
|
|
|
while (fgets(str, SIZE_LINE, file) != NULL) {
|
|
int length = strlen(str);
|
|
|
|
if (str[length - 1] == '\n') {
|
|
length--;
|
|
str[length] = '\0';
|
|
}
|
|
|
|
if (length < minLength) {
|
|
minLength = length;
|
|
strncpy(strmin, str, length);
|
|
strmin[length] = '\0';
|
|
}
|
|
}
|
|
|
|
return strmin;
|
|
} |