944,134 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1178
  • C++ RSS
May 10th, 2006
0

pls help

Expand Post »
hello,
am doing a project for my school . am asked to ,
1. open a file
2. tokensise the contents and then change the case of all the letters to lower case
3. search for all the words in tat file with another file
so there are 2 files one contails the file that is tokenised and the other a list of words (like a database) .
i need to search that database for all the words in the file that ve tokenised
the program i ve written is in c++ . am having a few problems with matching . can someone pls help

C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<string.h>
  3. #include<fstream.h>
  4. #include<conio.h>
  5. main()
  6. {
  7. // opening a file for reading
  8. ifstream in ("d:/programming/text.txt");
  9. if(!in)
  10. {
  11. cout<<"cannot open text \n";
  12. return 0;
  13. }
  14. char line[80];
  15. char* p;
  16. //opening a file for writing
  17. ofstream out("d:/programming/match.txt");
  18. if(!out){
  19. cout<<"cannot open match";
  20. return 1;
  21. }
  22. do{
  23.  
  24. in.getline(line,80);//getting the line from the file to be read
  25. p=strtok(line," " );//tokenising the file
  26. while(p){
  27. out<<p<<"\n";//putting all the tokens in the file for writing
  28. p=strtok(NULL," ");
  29. }
  30. }while (in);
  31. out.close();
  32. in.close();
  33. char line1[80];
  34. char line2[80];
  35. bool eof();
  36. ifstream in1 ("d:/programming/whitelist.txt");
  37. if(!in1)
  38. {
  39. cout<<"cannot open whitelist \n";
  40. return 1;
  41. }
  42. ifstream in2 ("d:/programming/match.txt");
  43. if(!in2)
  44. {
  45. cout<<"cannot open match \n";
  46. return 0;
  47. }
  48. do{
  49. in1.getline(line1,80);// get the tokens from the file
  50. cout<<"line1="<<line1<<"\n";
  51. do{
  52. in2.getline(line2,80); // get the token from the data base
  53. cout<<"line2="<<line2<<"\n";
  54. int p1=strcmp(line1,line2);//compare
  55. if(p1==0)
  56. {
  57. cout<<"match \n";
  58. }
  59. else{cout<<"no \n";}
  60. }while (in2 );
  61. cout<<"over\n";
  62. }while (in1 );
  63.  
  64.  
  65. in1.close();
  66. in2.close();
  67. getch();
  68. return 0;
  69. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
achala is offline Offline
13 posts
since May 2006
May 10th, 2006
0

Re: pls help

The way you are comparing looks to be ok.

Perhaps there's a problem with the tokenising part? Dunno not really looked at it properly, looks too much like C.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 10th, 2006
0

Re: pls help

#include<iostream.h>
#include<string.h>
#include<fstream.h>

You must be using an ancient c++ compiler -- maybe Turbo C++? If you want to learn c++ language you will have to toss that compiler into the bit bucket and get a modern one -- Dev-C++ is a good one.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
May 10th, 2006
0

Re: pls help

Toss out all that C code and replace it with c++. use std::string and >> insert operator and you don't have to do any tokenizing at all. Here's an example
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10. string word;
  11. // stay in the loop until you press Ctrl+Z <Enter>
  12. while( cin >> word )
  13. {
  14. // convert the word to lower-case
  15. transform(word.begin(),word.end(),word.begin(),::tolower);
  16. // now find the word in the database (not shown here)
  17. cout << word << endl;
  18. }
  19. cout << "done " << endl;
  20. return 0;
  21. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,963 posts
since Aug 2005
May 12th, 2006
0

Re: pls help

thankx it worked wel . now using trhe STL for most of the things
Reputation Points: 10
Solved Threads: 0
Newbie Poster
achala is offline Offline
13 posts
since May 2006

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: GUI Suggestions
Next Thread in C++ Forum Timeline: sequencer





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


Follow us on Twitter


© 2011 DaniWeb® LLC