| | |
Problem in reading a large file
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
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......................
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.
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
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:
3. Try to assign more sensible names for your variables...
That's all...
[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: C++ Syntax (Toggle Plain Text)
sstr.str(x);
C++ Syntax (Toggle Plain Text)
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
That's all...
•
•
Join Date: Oct 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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. Insertcout << sstr.str() << endl;statement aftersstr << 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:
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:C++ Syntax (Toggle Plain Text)
sstr.str(x);
3. Try to assign more sensible names for your variables...C++ Syntax (Toggle Plain Text)
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
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...............
) 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.
![]() |
Similar Threads
- Problem in reading Outlook Express DBX files ... (Visual Basic 4 / 5 / 6)
- c++ reading text file (C++)
- Reading large text file (C)
- Read file with unknown buffer length (C)
Other Threads in the C++ Forum
- Previous Thread: Adjacency Matrix and Linked lists
- Next Thread: Upload a file to a server in mfc using HTTP
Views: 978 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






