searching problem (help needed asap)

Pages: 123... 5
i need to search through a .dat file for the positions of a string but my code only reads the first line. the content of my file is below.

GETGGGGGGGGGATGGGGGGATETGGAEGETAEEEEGETEEAEEEAEAEEEAGGAGGATAEAGATAEAGAGAEATAGGAEATAGAGTAGAAGAGGAGA
AGAEAEEAGETGGAGAGGAAGETGAGAGGAGAGAGETGAGAGAGGAGAGAGGAGAGAGGETAGAGAGGAGAGGAGAGAEGAGAGGAGAGAGGETGAGAGAGGGETETTTTTTETAGGGET


Below is the fuction i created to do this but it just reads the first line alone

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
int find_get(string f_name, string u_file)
{
	
	
	ifstream yourfile (f_name); ///f_name is the string that holds the file 
	size_t search;              /// name
	if (yourfile.is_open())
	{ 
		while ( yourfile.good() )
		{
			getline (yourfile,u_file);						
			
			for ( int p = 1; p < u_file.length(); p++)
			{
				search = u_file.find("GET");
				if ( search!= string::npos)
				{
					cout <<search + 1<<",";
					
					break;
				}					
			}
		}	
	}
	return 0;
}


can a pro please help me see whats wrong, i thought it was the break; but removing it even makes it worse. NB. its not object oriented coding please thats too much for me for now. thanks in advance guys
please N>B the content of the file has about 7 lines
while ( yourfile.good() ) is bad.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// print out occurrances of string 'token' in file 'f_name'
void find_all( const std::string& f_name, const std::string& token )
{
    std::ifstream file(f_name) ;
    std::string line ;
    int line_num = 0 ;

    while( std::getline( file, line) )
    {
        ++line_num ; // line and column numbers start at 1

        std::size_t pos = 0 ;
        while( ( pos = line.find( token, pos ) )  != std::string::npos )
        {
            std::cout << "found in line " << line_num << " at col " << pos+1 << '\n' ;
            pos += token.size() ; // or ++pos
        }
    }
}
I know your tired of me, but i'm trying...

BTW, it's easier to keep up if you don't open a new post every time you ask the same question.

Your code works, to find the 1st match in both line 1 and line 2.
I had to change it a little to get it to compile.
I don't see how my changes would effect the results, but I dont have your entire program.

- The extra includes just make it quicker and easier for me to test code.
Remove what you dont need.
- All I changed should be flagged by // changed


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <conio.h>
#include <cstdlib>
#include <dos.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>
using namespace std;

ifstream yourfile;// changed
int linenumber=0;// changed

int find_get()// changed
{
yourfile.open("out.txt");// changed
string u_file;// changed
	size_t search;              /// name
	if (yourfile.is_open())
	{
cout << " File open" << endl; // changed
		while ( yourfile.good() )
		{
linenumber++;// changed
			getline (yourfile,u_file);
			for ( int p = 1; p < u_file.length(); p++)
			{
				search = u_file.find("GET");
				if ( search!= string::npos)
				{
				 cout << " Line#" << linenumber  << " String first found at position "<<search + 1 << endl;// changed

					break;
				}
			}
		}
	}
	return 0;
}


int main()
{
find_get();
return 0;
}



Output

C:\Temp>testxxx3
File open
Line#1 String first found at position 1
Line#2 String first found at position 9



This one find all instances of the string on all lines.

It may be messy, I didn't make any attempts to clean it up.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <conio.h>
#include <cstdlib>
#include <dos.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>
using namespace std;

ifstream yourfile;
int linenumber=0;
int found=0;
string newstring;
int newcount=0;
int TotalCount=0;

int find_get()
{
yourfile.open("out.txt");
string u_file;
size_t search;

		while ( yourfile.good() )
		{
        linenumber++;
        getline (yourfile,u_file);
		
        search = u_file.find("GET");
        if ( search!= string::npos)
		{
                newstring=u_file;
                newcount=search + 3;
                newstring.erase(0,newcount);
                TotalCount=TotalCount+search + 1;
                found=1;
                cout << "String: "<<  u_file << endl;
				cout << " Line#" << linenumber  << " String found at position " <<search + 1 << endl;
                
        while (found==1)
        {
                found=0;
                search = newstring.find("GET");
        if ( search!= string::npos)            
        {
                TotalCount=TotalCount+search + 3;
			    cout << " Line#" << linenumber  << " String found at position " <<TotalCount << endl;                

                newcount=search + 3;
                newstring.erase(0,newcount);
                found=1;
        }
        else
        {
            found=0;newcount=0;TotalCount=0;
            cout << "-------"<<endl;
        }
        }
        }
	}
	return 0;
}


int main()
{
find_get();
return 0;
}


Output:
C:\Temp>testxxx4
String: GETGGGGGGGGGATGGGGGGATETGGAEGETAEEEEGETEEAEEEAEAEEEAGGAGGATAEAGATAEAGAGA
EATAGGAEATAGAGTAGAAGAGGAGA
Line#1 String found at position 1
Line#1 String found at position 29
Line#1 String found at position 37
-------
String: AGAEAEEAGETGGAGAGGAAGETGAGAGGAGAGAGETGAGAGAGGAGAGAGGAGAGAGGETAGAGAGGAGAG
GAGAGAEGAGAGGAGAGAGGETGAGAGAGGGETETTTTTTETAGGGET
Line#2 String found at position 9
Line#2 String found at position 21
Line#2 String found at position 35
Line#2 String found at position 59
Line#2 String found at position 92
Line#2 String found at position 103
Line#2 String found at position 118
-------
Last edited on
thanks alot peeps. my code was right afterall but its doing the wrong thing. its only reading the position of the first occurence in each line. i want it to read the position of every occurence in all lines. please

thanks peeps, my code works afterall but its doing the wrong thing. it only searches for the position of the first occurence in each line rather than seaching for all occurence in all line.
i thing the "find" fuction is the problem... any idea of how to get this done?
please any help with the above problem?
Have you read this?
http://www.cplusplus.com/forum/general/88575/#msg475300

If you have, what problems are you having with it?

Hint: code which has a construct like
while ( yourfile.good() )
without a
if( !std::getline( yourfile, u_file ) ) break ;
inside the loop is not going to work correctly.
Last edited on
@JLB,i tried using ur example but it get even worse. i dont understand ur immediate post
> i tried using ur example but it get even worse.

It would be a lot easier if you posted the code that you tried, along with what are the errors that you got.


> i dont understand ur immediate post

1
2
3
4
5
6
7
8
9
10
while ( yourfile.good() ) // say the file is good() here
{
	std::getline (yourfile, u_file ); // but this getline fails
        // the file is in a failed state here, no line has been read
        // but we have not checked for it, and proceed as if a line 
        // was successfully read        

        // do something with u_file
        // ...
}
yeppie!!!!! JLB you are the best!!! seen my mistake. what about reading all content of a file as a whole string. so that the programme reads and search the content as a whole string not as an individual line even if the content has spaces in it.
> seen my mistake.

Another effective technique is to explain your code to someone else. This will often cause you to explain the bug to yourself. Sometimes it takes no more than a few sentences, followed by an embarrassed "Never mind, I see what's wrong. Sorry to bother you." This works remarkably well; you can even use non-programmers as listeners. One university computer center kept a teddy bear near the help desk. Students with mysterious bugs were required to explain them to the bear before they could speak to a human counselor.
- Brian W. Kernighan and Rob Pike in 'The Practice of Programming'



> what about reading all content of a file as a whole string.

For that, the file can't be huge - its contents have to fit into memory. And if line number information is required, the string would have to be parsed for new lines.

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <fstream>
#include <iterator>

std::string file_to_string( const std::string& path2file )
{
    std::ifstream file(path2file) ;
    std::istreambuf_iterator<char> begin(file), end ;
    return std::string( begin, end ) ;
}

Another effective technique is to explain your code to someone else. This will often cause you to explain the bug to yourself. Sometimes it takes no more than a few sentences, followed by an embarrassed "Never mind, I see what's wrong. Sorry to bother you." This works remarkably well; you can even use non-programmers as listeners. One university computer center kept a teddy bear near the help desk. Students with mysterious bugs were required to explain them to the bear before they could speak to a human counselor.
- Brian W. Kernighan and Rob Pike in 'The Practice of Programming'


This is so true. In a previous job, we used to call it the "Cardboard Programmer" phenomenon; you didn't really need an actual programmer to explain your problem to - a cardboard cut-out of a programmer would serve the same function :D
thanks for the tip mate.
is there no other way to do this... the above code seem complicated and does not work sort-off
Anybody wanna help me please....reading all content of a file as a whole string. so that the programme reads and search the content as a whole string not as an individual line even if the content has spaces in it.
> and does not work sort-off

Repeat: It would be a lot easier if you posted the code that you tried, along with the errors that you got.



> is there no other way to do this... the above code seem complicated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string file_to_string2( const std::string& path2file )
{
    std::ifstream file(path2file) ;
    std::string str ;
    char c ;
    while( file.get(c) ) str += c ;
    return str ;
}

std::string file_to_string3( const std::string& path2file )
{
    std::ifstream file(path2file) ;
    std::string str ;
    std::string line ;
    while( std::getline( file, line ) ) str += line + '\n' ;
    return str ;
}
@jlb thanks alot you are amazing. Can u send me your mail address so i can show you what exactly am trying to achieve?
> so i can show you what exactly am trying to achieve?

You would get better mileage if you post it here. Many eyes are better than one.
okay lets put it like this. like my first post says. i have a file that contains different letters of many lines (about 8 likes). i wanna get search for the file and get the position of a certain 3 letter string. but the output searchs the lines individually, thus getting a result like this.
" found at position : 5,21,34,25,5,45,23,5, " instead of somrthing like 5, 21,34,67,87,91,110 etc;
you can see that it finds the position 5 twice,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int find_get(string filepath )
{
    ifstream file(filepath ) ;
    string line ;
   string get= "GET";
while( std::getline( file, line) )
    {
        size_t pos = 0 ;
        while( ( pos = line.find( get, pos ) )  != string::npos )
        {
            cout << pos+1 <<",";
			f_out << pos+1 <<",";
            pos += get.size() ; 
        }
    }f_out<<endl;
	return 0;
}



what think should be done is getting is converting each letters into char and passing them into an array, line by line. but i dont know how to do this


i hope this explain it better. any suggestions
Pages: 123... 5