944,057 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2157
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 22nd, 2005
0

File array question

Expand Post »
Hey guys,

I have the following code, but for some reason it does not work with bigger txt files is this because of the size of the array?

And if so is there a simple way to solve the problem?

Thanks in advance for any help.

C++ Syntax (Toggle Plain Text)
  1. char ch[1000];
  2. int v_word = 0;
  3. int c_word = 0;
  4. ifstream file("c:/test.txt");
  5.  
  6. if (file.is_open())
  7. {
  8. while (file.getline(ch, 1000))
  9. if(strcmp(ch, "int") == 0)
  10. v_word++;
  11.  
  12. }
  13.  
  14. cout<<v_word<<endl;
  15. cout<<c_word<<endl;
  16. file.close();
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Dec 22nd, 2005
0

Re: File array question

it should work with text files of any size. Are any lines longer than 1,000 bytes? What is the purpose of checking if one of the lines = "int" ? Is there anything else following "int" on that line?

This is a c++ program -- why don't you use std::string instead of c-style arrays?
C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. ...
  3. std::string line;
  4. int v_word = 0;
  5. int c_word = 0;
  6. int spot;
  7. ifstream file("c:/test.txt");
  8.  
  9. if (file.is_open())
  10. {
  11. while (getline(fin,line) )
  12. if( (spot = line.find("int")) >= 0)
  13. v_word++;
  14. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Dec 22nd, 2005
0

Re: File array question

Hi thanks for your reply, The purpose of the int was just so i could count how many times it appears in the file it my full version also searches for other words. Could this be why it doesn't work on larger files?
I will look into strings.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Dec 22nd, 2005
0

Re: File array question

what do you mean by "it doesn't work"? what doesn't work? strcmp() compares the entire line with the text you put in literals, in your example it is "int". So if the line is "int some more stuff here", then strcmp() will fail. If you only want the first three characters, the use strncmp(), or if "int" can appear anywhere in the line use strstr(). Also you have to be careful with the comparison because strstr() will also find "intabc" is the same as "abcintabc" and " int ". So if strstr() finds the text "int" the program should check for space character immediately before and after. If there is no white space before and after, then strstr() did not find a word.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Dec 22nd, 2005
0

Re: File array question

Hi thanks for your reply. I have tried to use strstr() but I keep getting the wrong word count.

For example all the txt file contains is "hello james" and it gives the count 0 for both words.

C++ Syntax (Toggle Plain Text)
  1. ifstream file("c:\\test.txt");
  2.  
  3. if (file.is_open()) {
  4. while (file.getline(ch, 10000))
  5. {
  6. if(strstr(ch, "hello") == 0)
  7. v_word++;
  8.  
  9. if(strstr(ch, "james") == 0)
  10. c_word++;
  11. }
  12. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Dec 22nd, 2005
0

Re: File array question

C++ Syntax (Toggle Plain Text)
  1. if(strstr(ch, "hello") == 0)
The return value of strstr is a pointer to the match or a NULL pointer when no match was found. Here you are checking for a NULL pointer, meaning when no match was found, so you would be incrementing your counter at the wrong time.

Try this to find at least one occurence per line.
C++ Syntax (Toggle Plain Text)
  1. if(strstr(ch, "hello") != 0)
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 22nd, 2005
0

Re: File array question

Hi Dave, Thank you for your reply.

Is what you said also true for the strcmp() function?

Thanks.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Dec 22nd, 2005
0

Re: File array question

strcmp() return 0 if the comparison succeed, or non-zero if it fails.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Dec 22nd, 2005
0

Re: File array question

That what I thought but when I was tesing it if I used the code:

C++ Syntax (Toggle Plain Text)
  1. if(strcmp(ch, "hello") == 0)

I got the wrong number of times the word occured.

but if I used the following code for strcmp()

C++ Syntax (Toggle Plain Text)
  1. if(strcmp(ch, "hello") != 0)

it works. But with strcmp() shouldn't it have worked with the first piece of code?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Dec 22nd, 2005
0

Re: File array question

If the whole line is just "hello", then doing a strcmp against "hello" will be 0. Otherwise it won't be ("hello james" is not the same as "hello").

Even with strstr, you may want to move down the string after a match. Consider an input file like this:
C++ Syntax (Toggle Plain Text)
  1. hello james hello james hello hello hello james james hello james
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char ch[1000];
  9. int v_word = 0;
  10. int c_word = 0;
  11. ifstream file("test.txt");
  12.  
  13. if ( file.is_open() )
  14. {
  15. while ( file.getline(ch, 10000) )
  16. {
  17. char *match = ch;
  18. cout << "ch : " << ch << '\n';
  19. while ( (match = strstr(match, "hello")) != 0 )
  20. {
  21. cout << "<<hello>> " << match << '\n';
  22. v_word++;
  23. match += 5; /* the length of "hello" */
  24. }
  25.  
  26. match = ch;
  27. while ( (match = strstr(match, "james")) != 0 )
  28. {
  29. cout << "<<james>> " << match << '\n';
  30. c_word++;
  31. match += 5; /* the length of "james" */
  32. }
  33. }
  34. }
  35.  
  36. cout<<v_word<<endl;
  37. cout<<c_word<<endl;
  38. file.close();
  39. return 0;
  40. }
  41.  
  42. /* my output
  43. ch : hello james hello james hello hello hello james james hello james
  44. <<hello>> hello james hello james hello hello hello james james hello james
  45. <<hello>> hello james hello hello hello james james hello james
  46. <<hello>> hello hello hello james james hello james
  47. <<hello>> hello hello james james hello james
  48. <<hello>> hello james james hello james
  49. <<hello>> hello james
  50. <<james>> james hello james hello hello hello james james hello james
  51. <<james>> james hello hello hello james james hello james
  52. <<james>> james james hello james
  53. <<james>> james hello james
  54. <<james>> james
  55. 6
  56. 5
  57. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Win32 - Controls on main window
Next Thread in C++ Forum Timeline: vector<string> - way to find longest string?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC