Problem in reading a large file

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 6
Reputation: Headerandy is an unknown quantity at this point 
Solved Threads: 0
Headerandy Headerandy is offline Offline
Newbie Poster

Problem in reading a large file

 
0
  #1
Nov 12th, 2008
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............

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.....................
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.....

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......................
Last edited by Headerandy; Nov 12th, 2008 at 7:27 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem in reading a large file

 
0
  #2
Nov 12th, 2008
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:
  1. 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:
  1. while (getline(ifle,x)) { // use implicit stream to bool conversion
  2. if (x.find(s) != string::npos) // string::npos - "bad" position.
  3. cout << x << '\n'; // don't flush cout on every line
  4. }
  5. cout.flush(); // flush cout here
3. Try to assign more sensible names for your variables...
That's all...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 6
Reputation: Headerandy is an unknown quantity at this point 
Solved Threads: 0
Headerandy Headerandy is offline Offline
Newbie Poster

Re: Problem in reading a large file

 
0
  #3
Nov 14th, 2008
Originally Posted by ArkM View Post
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:
  1. 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:
  1. while (getline(ifle,x)) { // use implicit stream to bool conversion
  2. if (x.find(s) != string::npos) // string::npos - "bad" position.
  3. cout << x << '\n'; // don't flush cout on every line
  4. }
  5. 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...............
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem in reading a large file

 
0
  #4
Nov 14th, 2008
Originally Posted by Headerandy View Post
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...
Last edited by ArkM; Nov 14th, 2008 at 6:48 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 6
Reputation: Headerandy is an unknown quantity at this point 
Solved Threads: 0
Headerandy Headerandy is offline Offline
Newbie Poster

Re: Problem in reading a large file

 
0
  #5
Nov 21st, 2008
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......................
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem in reading a large file

 
0
  #6
Nov 21st, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 6
Reputation: Headerandy is an unknown quantity at this point 
Solved Threads: 0
Headerandy Headerandy is offline Offline
Newbie Poster

Re: Problem in reading a large file

 
0
  #7
Dec 3rd, 2008
Originally Posted by ArkM View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 978 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC