Hi friends ,
:(
I've made a programme that can read a file and if it finds a peticular string in the file it shows the whole
line. I've used "stringstream" to extractone by one and I checked that by the If loop on true condition it prints
the whole line............
:icon_question:
Now the problem is that It works well with a small file but if the file is very large say 4MB it skips some of
the initial line that contains the string by which I'm searching in file.....................:-O
Mainfact:

What is the problem I could not understand Am I mistaking somewhere in using stringstream to read a large file
Or there is other way of doing this...................
my programme is as below.....
:idea:
1. #include<iostream>
2. #include<fstream>
3. #include<sstream>
4.
5. using namespace std;
6.
7.int main()
8.{
9. string s,z,x;
10. stringstream sstr;
11. ifstream ifle("aspect.txt");
12.
13. cin>>s;
14.
15. while(!getline(ifle,x).eof()) {
16 sstr<<x;
17. while(sstr>>z) {
18. if(z==s) {
19. cout<<x<<endl;
20. }
21. }
22. }
23.
24. ifle.close();
25.
26. return 0;
27.}
Pls help me to get out of this problem......................

Recommended Answers

All 6 Replies

1. Next time use code tag with the language specifier for code snippets:
[code=cplusplus] your code

[/code]
2. You accumulate all input file contents line by line in the stringstream sstr variable. Insert cout << sstr.str() << endl; statement after sstr << x; and try this on a small input file. Of course, it's not a desired effect, it's an error. You must replace previous sstr contents by a next line:

sstr.str(x);

In actual fact no need in stringstream at all. Use find member function to detect substrung s in input line x. Also change main loop condition:

while (getline(ifle,x)) { // use implicit stream to bool conversion
    if (x.find(s) != string::npos) // string::npos - "bad" position.
        cout << x << '\n'; // don't flush cout on every line
}
cout.flush(); // flush cout here

3. Try to assign more sensible names for your variables...
That's all...

1. Next time use code tag with the language specifier for code snippets:
[code=cplusplus] your code

[/code]
2. You accumulate all input file contents line by line in the stringstream sstr variable. Insert cout << sstr.str() << endl; statement after sstr << x; and try this on a small input file. Of course, it's not a desired effect, it's an error. You must replace previous sstr contents by a next line:

sstr.str(x);

In actual fact no need in stringstream at all. Use find member function to detect substrung s in input line x. Also change main loop condition:

while (getline(ifle,x)) { // use implicit stream to bool conversion
    if (x.find(s) != string::npos) // string::npos - "bad" position.
        cout << x << '\n'; // don't flush cout on every line
}
cout.flush(); // flush cout here

3. Try to assign more sensible names for your variables...
That's all...

thanx ArkM my programme is now more efficient but it is still leaving first six -eight line for example my first line consist of ID but when I searched for this it started showing the contnts from eight line is that me who is making mistake or really this is true.............I'm sending an attachment of file which I had tried to read...............

thanx ArkM my programme is now more efficient but it is still leaving first six -eight line for example my first line consist of ID but when I searched for this it started showing the contnts from eight line is that me who is making mistake or really this is true.............I'm sending an attachment of file which I had tried to read...............

Well, and where is the attachment (zipped, I hope ;) ) and a corrected (but still incorrect ;) ) source?...

Have you ever heard about debuggers? I'm not sure that DaniWeb is the best debugging tool for C++ programs...

Thank Arkm I've heard of debugger but knows a little about them can you help me to debug this if you think I can........................
It is very nice to be with group of programmers in Daniweb......................

Simply present your code (with code tags ;) ) and problems description. Try to become familiar with your C++ installation debugger as soon as possible.

Good luck!

Simply present your code (with code tags ;) ) and problems description. Try to become familiar with your C++ installation debugger as soon as possible.

Good luck!

Thanx Arkm Thanx a trillion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.