#include "stdafx.h" #include <stdio.h> #include <iostream> #include <fstream> #include <string.h> #include <conio.h> int main() { char mass [80][80]; char slovo[80]; int i=0; int a,k=0; setlocale(LC_ALL, "Russian"); FILE *f; f=fopen("text.txt", "r"); if(f==NULL) printf("файл text.txt не открыт\n"); a=i; printf("Введите слово для поиска: "); // Здесь должен быть линейный поиск } Closed due to the fact that the essence of the issue is incomprehensible by the participants of Kromster , cheops , aleksandr barakin , user194374, D-side 15 Jun '16 at 14:39 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- to whom the essence of the question is not clear, look at the answers, if the answers are not clear, leave a comment under the corresponding answer - jfs
3 answers
Here's the answer, figure it out:
#include "iostream" #include <string> #include <fstream> using namespace std; int main(){ ifstream file("C://1.txt"); // открыли файл с текстом string s, find; char c; while (!file.eof()){ // прочитали его и заполнили им строку file.get(c); s.push_back(c); } file.close(); // обязательно закрыли cout << "enter a world for find: "; cin >> find; int pos = s.find(find); // поиск if (pos == -1) cout << "not finded" << endl; else cout << "finded in " << pos << " simvol position" << endl; return 0; } - Many thanks, now figured out. - AlfaC
- oneThis code is looking for a substring, not a word in the file, i.e. it will find “background” if the file contains “phone”, although the word “background” itself may not be in the file. It is necessary to check for errors after
file.get(c), otherwise you can write garbage on thesstring. Here is the working way of reading the file into the entire string . You can also mention that you need to change the code so that you can transfer Unicode file names and the input word to search on Windows. - jfs - @jfs immediately thought, you can add a couple of spaces at both ends, so that the word completely coincides, it is easy and you can also download according to the spaces - perfect
- one@perfect adding around the word spaces is not enough if the word is at the beginning, end of the file or if the words are separated by one of the other six standard whitespace characters:
" \t\n\r\f\v"(most likely with the new line'\n'). - jfs
istream_iterator<string> + find algorithm is a simple way to determine if a file contains a given space separated word using a linear search:
ifstream file("input.txt"); istream_iterator<string> eof; bool found = find(istream_iterator<string>(file), eof, word) != eof; For example, if the word is given from the command line, and the file is transmitted on the standard input:
/** $ g++ *.cxx -o find-word && <input.txt ./find-word word Exit status: 0 -- found word 1 -- not found 2 -- error */ #include <algorithm> #include <cstdlib> #include <iostream> #include <iterator> #include <string> int main(int argc, char* argv[]) { using namespace std; if (argc != 2) { cerr << "Usage: find-word WORD <input.txt\n"; exit(2); } string word(argv[1]); // word to search istream_iterator<string> words(cin), eof; bool found = find(words, eof, word) != eof; return found ? 0 : (cin.eof() ? 1 : 2); } The search works because istream_iterator<string> calls cin >> next_word inside, which skips the default spaces (the skipws flag is set) and the find algorithm then simply compares next_word == word .
What is a space, which means that such a word may depend on the current locale.
On systems with utf-8 locale, the code works as it is with arbitrary Unicode text (support for several languages in one document, support for emoticons, etc., although Unicode spaces are not recognized). Windows can spoil the input stream due to implicit (codepage) transformations of the byte stream - how to read the Unicode text on Windows is better to ask as a separate question.
The code reads only one word at a time and returns as soon as the input word is found, that is, the code can work with very large files and if the specified word is present in the input, the program can return earlier - without reading the entire input. The code also reports input errors ( cin.eof() test).
There are many string algorithms that help find a substring in a string , for example, the Aho-Korasik algorithm (could be used to implement fgrep ) can be more efficient in some cases than the naive linear search. Related question: Search for duplicate strings .
- The answer is scrupulous and accurate. It's time to create an object as a philosophy of programming, in which you would not be afraid of this word guru. - AlfaC
Read the file in a loop according to the fscanf() function and use strcmp() for comparison.
-
See man fscanf . What will be incomprehensible - ask.
Update
#include <stdio.h> #include <stdlib.h> #include <string.h> int main (int argc, char* argv[]) { FILE *fp; const char *fname; int rc = 1; // like grep return code if (!(fp = fopen(fname = (argv[1] ? argv[1]: "1.txt"),"r"))) { perror(fname); exit(2); } char input[81], word[81]; printf("Enter word: "); fflush(stdout); if (scanf("%80s", input) == 1) { printf("search [%s] in %s\n", input, fname); while(fscanf(fp, "%80s", word) == 1) if (strcmp(word, input) == 0) { rc = 0; printf("Found in pos: %ld\n", ftell(fp)); } } else puts("nothing to search"); return rc; } Windows 7, emacs e-shell:
c:/Users/avp/src/cc/hashcode $ g++ b.cpp -ob c:/Users/avp/src/cc/hashcode $ ./b Enter word: ERRDEMO search [ERRDEMO] in 1.txt Found in pos: 591 c:/Users/avp/src/cc/hashcode $ - I used your advice. But in retaliation, there was another problem, how to set an array so that all the text txt in 500 KB would fit into it. When you try to compile, swears. Actually, here is my code: int main () {char mass [100] [100]; <- how to set an array to fit all the text? char slovo [80]; int i = 0; int a, k = 0; setlocale (LC_ALL, "Russian"); FILE * f; f = fopen ("text.txt", "r"); if (f == NULL) printf ("text.txt file is not open \ n"); while (! feof (f)) {fscanf (f, "% s", & mass [i]); printf ("% s \ n", mass [i]); i ++; } // wrote a search} - AlphaH
- Use the string type better to load text into RAM, and then look for a string in the substring hashcode.ru/questions/107089/… - perfect
- @ AlphaCh, why read the whole text in an array? char word [80], slovo [80]; FILE * f = fopen (...); ... while (fscanf (f, "% s", word) == 1) if (strcmp (word, slovo) == 0) {puts ("Found"); break; // or what you want to do with this word} that's all. - avp
- It is necessary to read text into an array in order to perform a linear search. With file sizes, for example, 80 words, the search works, if the text is increased to 500 Kb, the program refuses to work. - AlfaC
- one@ AlphaCh, I think the problem is something else. By the way, what does it mean - refuses to work ? Describe in detail all the symptoms, messages on the screen, etc. (I do not possess the gift of clairvoyance to the full extent). - Try my fragment with a file of any size. (if it doesn't work again - put the code somewhere (for example on pastebin.com)) - avp