8 Replies - 512 Views - Last Post: 05 May 2013 - 03:08 PM Rate Topic: -----

#1 eig132062  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 04-March 13

C++ I want to find the words written to file1.txt in file2.txt.

Posted 04 May 2013 - 03:47 AM

    /* my code is suppose to print out a list of random four-letter strings and compare them to a second file of a list of actual four lettew ords to check how many of my randon strings are real words */
    
    static unsigned int Q[41790],indx=41790,carry=362436,xcng=1236789,xs=521288629;
    unsigned int refill()// refill f(n) for SuperKISS
    {
	int i;
	unsigned long long t;
	for (i=0;i<41790;i++)
	{
		t = 7010176ULL * Q[i] + carry;
		carry = (t>>32);
		Q[i] = ~t;
	}
	indx=1;
	return (Q[0]);
    }
    unsigned int SuperKISS()
    {
	xcng = 69069 * xcng + 123;
	xs ^= xs<<13;
	xs ^= xs>>17;
	xs ^= xs>>5;
	return (indx<41790 ? Q[indx++] : refill()) + xcng + xs;
    }

    char createRandstr(string alphaRand)
    {
        int stringLength = sizeof(alphaRand) - 1;
       
        char randstrn = alphaRand[rand() % stringLength];
        return randstrn;
    }

    int main()
    {
        string alphaRand = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
        srand(SuperKISS()*time(0));              
        int numofstrings = 10000;
        ofstream myfile("/Users/Projects/ScrabbleWordFinder/output.txt");

        if (myfile.is_open())
        {    
            for (int i=0; i < numofstrings; i++)
            {
            cout << createRandstr(alphaRand) << createRandstr(alphaRand) <<               createRandst(alphaRand) << createRandstr(alphaRand) << endl;
            myfile << createRandstr(alphaRand) << createRandstr(alphaRand) << createRandstr(alphaRand) << createRandstr(alphaRand) << endl;
            myfile.clear();
            }
            myfile.close();
            cout << endl;
            cout << "Done printing to file " << endl;
            }     
            else cout << "Unable to open file" << endl;
            exit(1); // terminate with error
            return 0;
    }
    /*the code i dont know how to make work is as follows are the follwing two, i dont know how to make it search the whole output file for more than one word at a time*/

    int main ()
    {
        string line;
        ifstream Myfile("/UsersProjects/ScrabbleWordFinder/output.txt");
        if(Myfile.is_open())
        {
            while(!Myfile.eof())
            {
                char* search = "LARD"; // search pattern
                int offset; // where it was found (or not (-1))
                getline(Myfile,line);
                cout<<line<<endl;
                // arg 2 (0) specifies the offset from where to begin seach
                if ((offset = line.find(search, 0)) != string::npos)
            }    
            cout << "found '" << search << "' @ offset " << offset << endl;
            cin.get();
        }
        else
        Myfile.close();
        cout<<"Unable to open this file."<<endl; 
        return 0;
    }
    /*Or something like this*/
    int main()
    {
        string Dest[3] = {"ZOON", "LARD", "NOON"} ;
        // Creating a ifstream object to read from the text file data.txt...
        ifstream     readFromFile("/Users/Projects/ScrabbleWordFinder/ScrabbleWordFinder/output.txt");

        // Creating a string variable.
        string output;
        string data;
        while(!readFromFile.eof())
        { 
            getline(readFromFile,data);
            for (int i = 0; i < 3; i++)
            {//find word in file  
                if (data.find(Desti[i])!=string::npos) cout << Dest[i] << endl;
                cout << data << endl;
            }    
        
        }
        return 0;
    } 







Is This A Good Question/Topic? 0
  • +

Replies To: C++ I want to find the words written to file1.txt in file2.txt.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: C++ I want to find the words written to file1.txt in file2.txt.

Posted 04 May 2013 - 07:13 AM

Do you have a problem or question? Don't ask questions in the code, ask them in the body of the post.

Do you know you have two functions named main() in your code? Only one function can be named main in a program.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 eig132062  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 04-March 13

Re: C++ I want to find the words written to file1.txt in file2.txt.

Posted 04 May 2013 - 12:46 PM

Sorry your right posting it in the body would make more sense my problem is i dont know how to find the words written to file1.txt and search for themone by one in file2.txt.(which is a list of four letter scrabble words) to verify that they are actual words. So far my code works for a few words but the list in file1.txt is several thousand random four letter strings that i need to compare to in file2.txt.The two mains is the result of latenight debugging and was trying to test different code snippets.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: C++ I want to find the words written to file1.txt in file2.txt.

Posted 04 May 2013 - 12:53 PM

Post your current code plus a small sample of your input files.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 eig132062  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 04-March 13

Re: C++ I want to find the words written to file1.txt in file2.txt.

Posted 04 May 2013 - 02:24 PM

Sample file1.txt is the following random words generated each time the code is ran
EWIK
GEHU
PSGL
UEAK
OAOT
CTWU
HOFT
OVWP
IIFF
VCTR
LHLS
KVQF
GMIH
DHAW
VGRD
JJHI
MSFB
LJEJ
GHKM
OILQ
VIDT
GHSU
BNPD
GOMP
BVHO
DMJU
HAKD
MKJR
POTQ


Sample File2.txt is the follwing list os scrabble words
AAHS
AALS
ABAC
ABAS
ABBA
ABBE
BAAL
BAAS
BABA
BABE
BABU
BABY
CAAS
CABA
CABS
CACA
CADE
CADI
YAAR
YABA
YACK
YADS
YAFF
YAGI
ZACK
ZAGS
ZANY
ZAPS
ZARF
ZATI

My code so far is
#include <string>
#include <ctime>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <random>

using namespace std;
/*
static unsigned int Q[41790],indx=41790,carry=362436,xcng=1236789,xs=521288629;
unsigned int refill()// refill f(n) for SuperKISS
{
	int i;
	unsigned long long t;
	for (i=0;i<41790;i++)
	{
		t = 7010176ULL * Q[i] + carry;
		carry = (t>>32);
		Q[i] = ~t;
	}
	indx=1;
	return (Q[0]);
}
unsigned int SuperKISS()
{
	xcng = 69069 * xcng + 123;
	xs ^= xs<<13;
	xs ^= xs>>17;
	xs ^= xs>>5;
	return (indx<41790 ? Q[indx++] : refill()) + xcng + xs;
}

char createRandstr(string alphaRand)
{
    int stringLength = sizeof(alphaRand) - 1;
    //for (int i=0;i<4;i++)
    //{
        char randstrn = alphaRand[rand() % stringLength];
        return randstrn;
    //}
    
}

int main()
{
    string alphaRand = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //mt19937 mt;//mersenne twister 2^(19937-1) does not work on on compilers w/o C++11
    //mt.seed(time(0));
    srand(SuperKISS()*time(0));/*found it online @ www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG*/
    int numofstrings = 10000;
    ofstream myfile("/Users/eknargarcia/Projects/FourWordFinder/FourWordFinder/output.txt");

    if (myfile.is_open())
    {
        for (int i=0; i < numofstrings; i++)
        {
            cout << createRandstr(alphaRand) << createRandstr(alphaRand) << createRandstr(alphaRand) << createRandstr(alphaRand) << endl;
            myfile << createRandstr(alphaRand) << createRandstr(alphaRand) << createRandstr(alphaRand) << createRandstr(alphaRand) << endl;
            myfile.clear();
        }
        myfile.close();
        cout << endl;
        cout << "Done printing to file " << endl;
    }
    else cout << "Unable to open file" << endl;
//this is the code  below i cant make work

string randWords[3] = {"ZOON", "LARD", "NOON"};
// Creating a ifstream object to read from the text file data.txt...
    ifstream readFromFile("/Users/Projects/ScrabbleWordFinder/ScrabbleWordFinder/output.txt");

    // Creating a string variable.
    string data;
    

    while(!readFromFile.eof())
    { 
        getline(readFromFile,data);
        for (int i = 0; i < 3; i++)
        {        
        	if (data.find(randWords[i])!=string::npos) cout <<randWords[i] << endl;
            if (output.find(randWords[i])!=string::npos) cout << randWords[i] << endl;
    		cout << data;
    	}	
    }
	cout << "Done" << endl;


 return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

Re: C++ I want to find the words written to file1.txt in file2.txt.

Posted 04 May 2013 - 02:37 PM

Hi, the strings in a file will need to be read multiple times, so it makes sense to load one file into memory.

Could you use a vector of string? vector
Was This Post Helpful? 1
  • +
  • -

#7 eig132062  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 04-March 13

Re: C++ I want to find the words written to file1.txt in file2.txt.

Posted 04 May 2013 - 02:48 PM

Yeah i saw a lot of similar solutions on google using vectors, i'm just not familiar with them so i wouldn't know where to begin
Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 278
  • View blog
  • Posts: 1,783
  • Joined: 20-September 08

Re: C++ I want to find the words written to file1.txt in file2.txt.

Posted 04 May 2013 - 02:53 PM

This example of a function that you could use ...

may give you a start using C++ vector ...

void fillWithRan4LetWords( vector< string >& v, int numWords )
{
    string source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int len = source.length();
    srand( time(0) );

    for( int i = 0; i < numWords; ++ i )
    {
        string wordLet4 = "xxxx";
        for( int j = 0; j < 4; ++ j )
             wordLet4[j] = source[ rand() % len ];
        v.push_back( wordLet4 );
    }
}


Was This Post Helpful? 0
  • +
  • -

#9 eig132062  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 04-March 13

Re: C++ I want to find the words written to file1.txt in file2.txt.

Posted 05 May 2013 - 03:08 PM

Thanks David and #define for your generous responses, this is the solution i went with, my problem now is how can i implement a word counter of the words found in the output.txt file, with the following code


void checkIsWordFound(string wordToFind,string text)
{
	if(text.find(wordToFind)!=string::npos)
	cout << "FOUND WORD: << wordToFind << endl;
	else
	for(unsigned int i=0;i<wordToFind.size();i++)
	cout << "Word not found ";
}
int main()
 {
	 ifstream firstFile("/Users/Projects/FourWordFinder/FourWordFinder/output.txt");
	 ifstream secondFile("/Users/Projects/FourWordFinder/FourWordFinder/input.txt");
	 string fFileStr,sFileStr;
	 
	 if(firstFile&&secondFile)
	 {
		 while(firstFile)
		 fFileStr += (char)firstFile.get();
		 while(secondFile)
		 sFileStr += (char)secondFile.get();
		 cout << "This is the content of the first file " << endl;
		 string tmpStr;
		 for(unsigned int i=0;i<fFileStr.size();i++)
		 {
			if(fFileStr[i]!=' '&&fFileStr[i]!='\n')
			tmpStr += fFileStr[i];
			else
			{
				checkIsWordFound(tmpStr,sFileStr);
				tmpStr = "";
				if(fFileStr[i]==' ')cout << " ";
				if(fFileStr[i]=='\n')cout << endl;
		 	}
	 	}
	 
	 }
	 else cout << "Cannot open the files " << endl;
 }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1