The file output with selection of the shortest words in C++

The question was asked: 6 years 10 months ago   views: 36

Tell me what was the error, why the short function words is not working and how to insert the selection of words in the text the smallest?

int ProvRazm(); int Vivodnaekran(); int SamKorotkSlov(); int main() { setlocale(0, "rus"); string buf; ifstream file("text.DAT"); /* Open file */ if (!file.is_open()) { /* Check file */ cerr << "File no. Check the file name or its presence!" << endl; getchar(); return 1; } ProvRazm(); Vivodnaekran(); SamKorotkSlov(); system("pause"); return 0; } int ProvRazm() { FILE *in = fopen("text.DAT", "r"); // open a stream // with the parameter "read-only" int sum = 0; char buf[72]; while (!feof(in)) // checking the size of the text { fgets(buf, 72, in); // f-tsiya COP. one line from a file if (strlen(buf) > 70) // check string length { cout << "row Size too large(not over 70).\n"; } sum += strlen(buf); // f-tsiya counting the number of characters in the string } if (sum > 1000) { cout << "a Large number of characters (more than 1000).\n"; } fclose(in); // closing the stream getchar(); return sum; // return the number of characters in the file } int Vivodnaekran() { string buf; ifstream file("text.DAT"); cout << "file Contents:" << endl; while (getline(file, buf)) { cout << buf << endl; } return 1; } int SamKorotkSlov() { char str[1000]; // curlen current length of the word // maxlen - maximum length of words // index - the index corresponding to the position // end the longest word +1 int curlen = 0, maxlen = 0, index = 0, i; ifstream file("text.DAT"); for (i = 0;; i++) { if (str[i] == '' || str[i] == '\0') { if (curlen > maxlen) { maxlen = curlen; index = i; } // if the end of the string -> exit the loop if (str[i] == '\0') break; curlen = 0; } else curlen++; } cout << "" << maxlen << endl; getchar(); }
In your function SamKorotkSlov() (at least here in the above code), there are no input data from the file into str[]. Why do You open the file many times and in different styles (C++, C)? Just one, then it can be re-read from the beginning. -In fact (assuming that the word is also limited by the end of the line) a single pass of reading the file line by line. Along with checking the length of a string, you can choose from it the words and store the shortest (You know, there might be several) in the array. At the end you can print them from this array. What is not clear - ask. - 11-11-2012 в 18:37:27

Answers   1


As I understand it You need to find the word with maximum length, although the name of the function, with a minimum of:) Here is an example for a search with a minimum length of:

#include <iostream> #include <fstream> #include <string> #include <limits> using namespace std; int main() { setlocale(0, "rus"); string buf; ifstream file("text.DAT"); /*Open file*/ if(!file.is_open()) /*Check file*/ { cerr<<"File not present!!check the file name!or his presence!"<<endl; system("pause"); return 1; } unsigned int min = INT_MAX; string wordMinLength; while(file >> buf) { if (buf.length() < min) { wordMinLength = buf; min = buf.length(); } } file.close(); cout << min << "" << wordMinLength; system("pause"); return 0; }

To search with a maximum length change the line

unsigned int min = INT_MAX; if (buf.length() < min)


unsigned int min = 0; if (buf.length() > min)

Well, rename the variables for readability.


For highlighted words with a minimum length, use the function SetConsoleTextAttribute. For example according to the following algorithm: remember the length of the minimal words in the text, and then printing to console the text one word at a time (do not forget about punctuation and spaces) and, if the word length is equal to the minimum, then set the text color, e.g. to red, displaying the word and then return the white color. Something like this:

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); ... while (file >> buf) { ... if (buf.length() == min) // Here you can still check a word or a punctuation mark or special character { SetConsoleTextAttribute(hConsole, 12); // Set red } cout << buf; // Print the word SetConsoleTextAttribute(hConsole, 7); // Return white ... }
Answered: 11-11-2012 в 10:25:10
no you are right with the minimum long you need to find, thank you very much for the help, and how to highlight these words in the text, when you press any buttons I don't understand how to do it - 11-11-2012 в 11:22:04
In the text, I mean in the file? - 11-11-2012 в 11:32:37
and it's why that little word seeks in one line, and how to make all the Teks, please help - 11-11-2012 в 11:38:23
Generally it is strange. I looking for the file. - 11-11-2012 в 11:57:23
@Daniel, (as I understand it Your answer) and why such a strange ...|| min == -1) to find minimum? In General, the highs-lows of the types prescribed in and int is INT_MAX. You take the unsigned min, and assigning it to -1 make it UINT_MAX. Thus, any word length (buf.length() and the first including) will be less than the initial min (and everything will work without a check for -1). - 11-11-2012 в 18:50:32