Thread: Comparing two files, if two characters on the same col don't match, cout it. Works

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Comparing two files, if two characters on the same col don't match, cout it. Works

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <fstream>
    #include <string>
    
    int main()
    {
    
    std::ifstream tfile("C:/test.dat");
    std::ifstream tfile2("C:/test2.dat");
    
    char tdata[15000000];
    char tdata2[15000000];
    
    int i = 0;
    int x = 0;
    
    while (!tfile.eof() && i < sizeof tdata)
    {
    tfile.get(tdata[i++]);
    }
    
    while (!tfile2.eof() && x < sizeof tdata2)
    {
    tfile2.get(tdata2[x++]);
    }
    
    string kokane(tdata);
    string kokane2(tdata2);
    
    
    for(int i=0; i<=(kokane.length() | kokane2.length()); i++)
    {
    
      if(! (kokane.substr(i, 1) == kokane2.substr(i, 1)) )
      {
      cout << "char " << i << ": " << kokane.substr(i, 1) << " !== " << kokane2.substr(i, 1);
      }
    
    }
    
    system("PAUSE");
    return 0;
    }
    This code worked better before, but then I messed with it. Anyway, the code worked perfectly on small files, but the files I want to compare are 700-800k big. I don't know if there are characters in those files which the program can't take or if it is something else.

    If anyone knows what might be the problem, or if there's any real program for this, please let me know.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    66
    You are allocating way too much on the stack, move the big buffers to the frame and see if that helps.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    You are wasting wayyyyyyyyyyy to much memory, and then you create strings when you dont need to.

    By limiting tdata you place a limit on the tile size. If you want to compare 2 different files try this

    Code:
    ifstream input1("file1");
    ifstream input2("file2");
    double charcount = 0;
    char buf1,buf2;
    do
    {
     input1.get(buf1);
     input2.get(buf2);
     charcount ++;
     if( buf1 != buf2) 
     {
        cout << "Char #:" << charcount << ":" << buf1 << " != " << buf2 << endl;
     }
    }while( (! input1.eof() ) && (! input2.eof() ) );

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Thanks!

    I learned allocating memory dynamically and all that just a few hours ago
    Last edited by Zewu; 06-19-2002 at 02:57 PM.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    I found out that that only works till 340 bytes though. What's wrong here, can't the char's hold anymore memory? How can it be solved?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Comparing Files
    By snowy101 in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2002, 10:43 AM
  4. How do I match 2 files to print data.
    By sketchit in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-12-2001, 05:45 PM
  5. How do you match 2 files?
    By sketchit in forum C Programming
    Replies: 1
    Last Post: 11-10-2001, 08:06 PM