File array question

Reply

Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

File array question

 
0
  #1
Dec 22nd, 2005
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.

  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();
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: File array question

 
0
  #2
Dec 22nd, 2005
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?
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: File array question

 
0
  #3
Dec 22nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: File array question

 
0
  #4
Dec 22nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: File array question

 
0
  #5
Dec 22nd, 2005
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,314
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: File array question

 
0
  #6
Dec 22nd, 2005
  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.
  1. if(strstr(ch, "hello") != 0)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: File array question

 
0
  #7
Dec 22nd, 2005
Hi Dave, Thank you for your reply.

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

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: File array question

 
0
  #8
Dec 22nd, 2005
strcmp() return 0 if the comparison succeed, or non-zero if it fails.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: File array question

 
0
  #9
Dec 22nd, 2005
That what I thought but when I was tesing it if I used the code:

  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()

  1. if(strcmp(ch, "hello") != 0)

it works. But with strcmp() shouldn't it have worked with the first piece of code?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,314
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 229
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: File array question

 
0
  #10
Dec 22nd, 2005
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:
  1. hello james hello james hello hello hello james james hello james
  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. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC