954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem in reading a large file

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
2. #include
3. #include
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<>z) {
18. if(z==s) {
19. cout<

Headerandy
Newbie Poster
6 posts since Oct 2008
Reputation Points: 10
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. 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...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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

Headerandy
Newbie Poster
6 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 
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...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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

Headerandy
Newbie Poster
6 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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!

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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

Headerandy
Newbie Poster
6 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You