C++ Program to read and compare two files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zr3cool
    New Member
    • Apr 2008
    • 3

    #1

    C++ Program to read and compare two files

    heres the program description....

    There exist two text files that are not identical with a size of between 100 and 250 characters. Write a c++ program that will read in these two text files, prompt the user for a length factor some value between 5 and 20 and locate similar (words phrases) that are common to both files. print the results of both files and similar words between them.

    so basically the program asks for a length factor value between 5 and 20 (lets say 6 which is school) and i have to text files text1.txt and text2.txt (text1 has amy goes to school and text2 has mom goes to school) the program will print school.

    HELP ME PLEASE...
    i know how to read from a text file but i dont know how to compare it..
    thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First, can you identify a word?

    If not, write a function to parse one word from one of your files.

    Calling it again, gets the nest word.

    I would assume you could put these words in an array.

    Then sort the array.

    Then call your function using the other file and see if the word is already in the array.

    What do you think?

    Comment

    • zr3cool
      New Member
      • Apr 2008
      • 3

      #3
      sounds really good...but im having trouble putting everything together its driving me crazy.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        So you have the pieces independent of each other? Can you post each of them? We can help you put them together.

        Comment

        • zr3cool
          New Member
          • Apr 2008
          • 3

          #5
          this is the code that will read a text file...i just a way to compare it and print the results. heres the structure:

          string file1;
          // TODO: Load the first file into file1

          string file2;
          // TODO: Load the second file into file2

          int lengthFactor;
          // TODO: Get the length factor from the user

          for(i=0;i<file1 .length();++i)
          { if( file2.find( file1.substr( i, lengthFactor) ) != npos )
          { // file1.substr(i, lengthFactor) was found in file2 so do whatever you want with it
          }
          }//end for(i)


          AND HERE IS THE CODE TO READ A FILE

          #include "stdafx.h"
          #include<iostre am>
          #include<fstrea m>
          #include<string >

          using namespace std;
          int main()
          {
          static char ph[500];
          int a;
          char y;
          const char* filename="c:\\t ext1.txt";
          ifstream infile(filename );
          if (!infile) cout<<"no input file"<<endl;
          a=0;
          while ( (ph[a++] =infile.get()) != EOF );

          cout <<ph <<" "<<--a;
          infile.close();
          cout<<"\n\nEOJ" ;
          return 0;
          }

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Have you researched what ifstream::get() does?

            Have you considered the >> operator?

            Comment

            Working...