problem with files

how i can search a sub string in a file?
length of sub string is Variable and the number of search is indefinite.
Last edited on
this may not be the most efficient manner to do this, but here is what i would do:
read the file into a buffer of say 256/512 chars, the search for the sub-string linearly.
There is probably a std method that would solve the problem but you could just extract char by char and use a loop of if statements to check if all the chars match.
Last edited on
file is contained 450000 words.
in each case i should doing this works?
what is the fastest way?
> what is the fastest way?

Depends on what should be the result of the search for the sub-string.
For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string>
#include <fstream>
#include <iterator>
#include <algorithm>

// return true if the file contains substr
bool contains( const std::string& path_to_file, const std::string& substr )
{
    std::ifstream file(path_to_file) ;
    std::istreambuf_iterator<char> begin(file), end ;
    return std::search( begin, end, substr.begin(), substr.end() ) != end ;
}

// return all the lines in the file containing substr
std::vector<std::string> lines_with( const std::string& path_to_file, const std::string& substr )
{
    std::vector<std::string> result ;

    std::ifstream file(path_to_file) ;
    std::string line ;
    while( std::getline( file, line ) )
        if( line.find(substr) != std::string::npos )
            result.emplace_back( std::move(line) ) ;

    return result ;
}
can't really use std::search with std::istreambuf_iterator (although it works with gcc/stdlibc++, it fails to compile with clang/libc++ for example)
> can't really use std::search with std::istreambuf_iterator

Yes. std::search() requires a forward iterator.

Thanks, Cubbi.
Thank's.
but how i can solve it?
when i use JLBorges's way errors occurring.please help me it is very important.each run i would opening the file 100 time.
my file is in below form:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
a
aa
Aachen
aardvark
aardvarks
aardwolf
aardwolves
Aarhus
Aaron
Aaronic
Aaronical
Aaron's beard
Aaron's rod
A'asia
aasvogel
aasvogels
Ab
aba
abac
abaca
abacas
abaci
aback
abacs
abactinal
abactinally
.
.
.
.
.
.
. 
> but how i can solve it?

Solve what?

What are you searching for? And what should be returned from the search?


> each run i would opening the file 100 time.

Why? That seems like a silly thing to do for a 2.5 MB file. Why can't it be read once and then its contents held in memory?
i'm using VS for programming.
error:namespace "std" has no member "vector"
searching for patterns i have in table.
return true(exist)/false(not exist).
dear JLBorges i'm beginner .
>Why can't it be read once and then its contents held in memory?
how can I do this?
> searching for patterns i have in table.

Are you searching for complete words eg. incomprehensible or any sequence of characters eg. mpreh?
Is your search case sensitive?
one table:
i want to search for example (dnnssttoor OR dnnsstt) if this word dos not exist search function return false and if exist return true, also each time running program this give me a different table from previous
a a r d w o l v e s
a b a n d o n e e s
a b a n d o n i n g
a b a s e m e n t s
a b a s h m e n t s
a b a t e m e n t s
a b a t - j o u r s
A b b o t s f o r d
a b b o t s h i p s
a b b r e v i a t e
It is still not clear what you are trying to do.

> i want to search for example (dnnssttoor):

The exact word 'dnnssttoor'?
A word containing the substring 'dnnssttoor'?
A word containing exactly one 'd', two 'n', two 's', two 't' etc.?
A word containing at least one 'd', two 'n', two 's', two 't' etc.?


And what does this mean: 'each time running program this give me a different table from previous'?
> i want to search for example (dnnssttoor)
for 4th column at below :
i want search for :dnnssttoor OR dnnssttoo OR dnnsstto OR dnnsstt...
OR d.

>And what does this mean: 'each time running program this give me a different table from previous'?
at below you see two different table.
********************
a a r d w o l v e s
q b a n d o n e e s
e b a n d o n i n g
r b a s e m e n t s
j b a s h m e n t s
a b a t e m e n t s
z b a t - j o u r s
b b b o t s f o r d
e b b o t s h i p s
w b br e v i a t e
********************
a b d i c a t i n g
v b d i c a t i o n
s b d i c a t o r s
xb d o m i n a l s
m b d o m i n o u s
n b d u c t i o n s
y b e r d e v i n e
d b e r d o n i a n
s b e r r a t i n g
a b e r r a t i o n
Ok.

First, read the all the words in /usr/share/dict/words into a vector of strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <fstream>
#include <string>

// read all the lines in the file into a vector
std::vector<std::string> lines_in_file( const char* path_to_file )
{
    std::vector<std::string> result ;

    std::ifstream file(path_to_file) ;
    std::string line ;
    while( std::getline( file, line ) ) result.push_back(line) ;

    return result ;
}

int main()
{
    const char* const path = "/usr/share/dict/words" ;
    const std::vector<std::string> words = lines_in_file(path) ;
}


Now, the char at column four of line 134 (zero-based) is: words[134][4].
Thanks a lot.
Topic archived. No new replies allowed.