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

#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<conio.h>
main()
{
 // opening a file for reading
 ifstream in ("d:/programming/text.txt");
 if(!in)
 {
  cout<<"cannot open text \n";
  return 0;
 }
 char line[80];
 char* p;
 //opening a file for writing
  ofstream out("d:/programming/match.txt");
           if(!out){
                    cout<<"cannot open match";
                    return 1;                                      
           }
 do{
  
  in.getline(line,80);//getting the line from the file to be read
  p=strtok(line," " );//tokenising the file
  while(p){
  out<<p<<"\n";//putting all the tokens in the file for writing
  p=strtok(NULL," ");
        }
 }while (in);
 out.close();
 in.close();
 char line1[80];
 char line2[80];
 bool eof();
 ifstream in1 ("d:/programming/whitelist.txt");
 if(!in1)
 {
  cout<<"cannot open whitelist \n";
  return 1;
 }
 ifstream in2 ("d:/programming/match.txt");
 if(!in2)
 {
  cout<<"cannot open match \n";
  return 0;
 }
 do{
  in1.getline(line1,80);// get the tokens from the file 
        cout<<"line1="<<line1<<"\n";
        do{  
       in2.getline(line2,80); // get the token from the data base 
             cout<<"line2="<<line2<<"\n";
             int p1=strcmp(line1,line2);//compare 
       if(p1==0)
             {
                      cout<<"match \n";
             }
         else{cout<<"no \n";}     
  }while (in2 );
  cout<<"over\n";
 }while (in1 );
 
 
 in1.close();
 in2.close();
 getch();
 return 0;
}

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

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.

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

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

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;


int main()
{
	string word;
	// stay in the loop until you press Ctrl+Z <Enter>
	while( cin >> word )
	{
		// convert the word to lower-case
		transform(word.begin(),word.end(),word.begin(),::tolower);
		// now find the word in the database (not shown here)
		cout << word << endl;
	}
	cout << "done " << endl;
	return 0;
}

thankx :) it worked wel . now using trhe STL for most of the things

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.