pls help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2006
Posts: 13
Reputation: achala is an unknown quantity at this point 
Solved Threads: 0
achala achala is offline Offline
Newbie Poster

pls help

 
0
  #1
May 10th, 2006
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: pls help

 
0
  #2
May 10th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: pls help

 
0
  #3
May 10th, 2006
#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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: pls help

 
0
  #4
May 10th, 2006
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 13
Reputation: achala is an unknown quantity at this point 
Solved Threads: 0
achala achala is offline Offline
Newbie Poster

Re: pls help

 
0
  #5
May 12th, 2006
thankx it worked wel . now using trhe STL for most of the things
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC