Results 1 to 4 of 4
  1. #1
    why06jz's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Posts
    295
    Reputation
    14
    Thanks
    54

    First time I got one of these errors.

    File Comparator Fails when I enter Files

    Usually when I'm programming I'll get some compiler errors. Maybe even a logic error or two, but this is the first time this happened.

    Approximately 9:00am EST I ran my program, happily delighted that I got all of the compile time errors out of it and then this happens.


    As soon as I enter the file names the whole thing blows up in my face!

    I checked the code over again and again and I can't seem to find the problem. It runs fine until I enter the file names. Then the whole thing shuts down. Can anyone help me out! D;

    Here I'll give you the Complete source. It is well annotated. I was trying to make a File Comparator, but as soon as you enter the files the program crashes, some comparator right o_O? Thanks in advanced.
    Code:
    /* File Comparator
    Created by: Shaun Carter (why06)
    Date: 10/03/2009*/
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int CompareFiles(ifstream &file1, ifstream &file2);
    
    int main(int argc, char argv[])
    {
        char* file[2];
        char* files;
        
        bool filesCompared = false;
        
        while(!filesCompared)
        { // Starting message    
         cout << "Enter: File1,File2\n";
         cin >> files;
         
         // Will try to parse the text into different files
         try{
         int p = 0;
         for(int i = 0; files; i++)
         {
                 if(files[i]==',')
                 {
                   p++; continue;
                 }
                 else file[p]+=files[i];
                 if(p>1)throw(1);
         }
         // If more then two files are given this will cathc it.
         }catch(int){cout << "Only compare two files" << "This program will ONLY compare the firt two you entered\n" ; return 1;}
         
         //Declarinng my fstream variables that I will use for the binary comparison//
         ifstream f1(file[0], ios::in | ios::binary);                                //
         if(!f1){                                                                   //
                 cout << "Cannot open first file.\n";                               //
                 return 1;                                                          //
         }                                                                          //
         ifstream f2(file[1], ios::in | ios::binary);                                //
         if(!f2){                                                                   //
                 cout << "Cannot open second file.\n";                               //
                 return 1;                                                          //     
         }                                                                          //
         /////////////////////////////////////////////////////////////////////////////
         
         //Execution passes to CompareFiles. Will return 0 (are equal), 1 (file1 is larger), 2 (file2 is larger)
         cout << "Comparing...\n";
         int result = CompareFiles(f1, f2);
         
         // Finally returs result of Comparison.
         if(result)
         {
                   cout << "Files are not the same." << "\n";
                   if(result == 1)cout << file[1] <<" is larger then " << file[0] << ".\n";
                   else cout << file[0] <<" is larger then " << file[1];
         }
         else cout << "These files are the same.";
         filesCompared = true;
        }
        system("pause");
        return 0;
    }
         
         int CompareFiles(ifstream &file1, ifstream &file2)
         {
             char buf1[50];
             char buf2[50];
             
             file1.read( buf1, sizeof buf1);
             file2.read( buf2, sizeof buf2);
             
             if(file1.gcount() != file2.gcount())
             {
              cout << "Differing sizes...\n";
              if(file1.gcount() > file2.gcount()) return 1;
              else return 2;
             }
             else
             {
                 cout << "Same size. Running binary comparison...";
                 // Left off here
                 return 0;
             }
         }

  2. #2
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    Could files being a null pointer (never initialized) have something to do with it?
    Also i'd say your for loop that follows would make it go out of bounds and throw an access violation
    Last edited by B1ackAnge1; 10-04-2009 at 02:17 PM.

  3. #3
    why06jz's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Posts
    295
    Reputation
    14
    Thanks
    54
    Quote Originally Posted by B1ackAnge1 View Post
    Could files being a null pointer (never initialized) have something to do with it?
    Also i'd say your for loop that follows would make it go out of bounds and throw an access violation
    I think I sort of rushed the design of this thing too. I'll take a look at it again with what you said in mind. It's amazing that I've been so busy to day doing nothing at all o_O?!

  4. #4
    jesussoto100's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    52
    Reputation
    10
    Thanks
    0
    My Mood
    Aggressive
    Quote Originally Posted by why06jz View Post
    File Comparator Fails when I enter Files

    Usually when I'm programming I'll get some compiler errors. Maybe even a logic error or two, but this is the first time this happened.

    Approximately 9:00am EST I ran my program, happily delighted that I got all of the compile time errors out of it and then this happens.


    As soon as I enter the file names the whole thing blows up in my face!

    I checked the code over again and again and I can't seem to find the problem. It runs fine until I enter the file names. Then the whole thing shuts down. Can anyone help me out! D;

    Here I'll give you the Complete source. It is well annotated. I was trying to make a File Comparator, but as soon as you enter the files the program crashes, some comparator right o_O? Thanks in advanced.
    Code:
    /* File Comparator
    Created by: Shaun Carter (why06)
    Date: 10/03/2009*/
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int CompareFiles(ifstream &file1, ifstream &file2);
    
    int main(int argc, char argv[])
    {
        char* file[2];
        char* files;
        
        bool filesCompared = false;
        
        while(!filesCompared)
        { // Starting message    
         cout << "Enter: File1,File2\n";
         cin >> files;
         
         // Will try to parse the text into different files
         try{
         int p = 0;
         for(int i = 0; files; i++)
         {
                 if(files[i]==',')
                 {
                   p++; continue;
                 }
                 else file[p]+=files[i];
                 if(p>1)throw(1);
         }
         // If more then two files are given this will cathc it.
         }catch(int){cout << "Only compare two files" << "This program will ONLY compare the firt two you entered\n" ; return 1;}
         
         //Declarinng my fstream variables that I will use for the binary comparison//
         ifstream f1(file[0], ios::in | ios::binary);                                //
         if(!f1){                                                                   //
                 cout << "Cannot open first file.\n";                               //
                 return 1;                                                          //
         }                                                                          //
         ifstream f2(file[1], ios::in | ios::binary);                                //
         if(!f2){                                                                   //
                 cout << "Cannot open second file.\n";                               //
                 return 1;                                                          //     
         }                                                                          //
         /////////////////////////////////////////////////////////////////////////////
         
         //Execution passes to CompareFiles. Will return 0 (are equal), 1 (file1 is larger), 2 (file2 is larger)
         cout << "Comparing...\n";
         int result = CompareFiles(f1, f2);
         
         // Finally returs result of Comparison.
         if(result)
         {
                   cout << "Files are not the same." << "\n";
                   if(result == 1)cout << file[1] <<" is larger then " << file[0] << ".\n";
                   else cout << file[0] <<" is larger then " << file[1];
         }
         else cout << "These files are the same.";
         filesCompared = true;
        }
        system("pause");
        return 0;
    }
         
         int CompareFiles(ifstream &file1, ifstream &file2)
         {
             char buf1[50];
             char buf2[50];
             
             file1.read( buf1, sizeof buf1);
             file2.read( buf2, sizeof buf2);
             
             if(file1.gcount() != file2.gcount())
             {
              cout << "Differing sizes...\n";
              if(file1.gcount() > file2.gcount()) return 1;
              else return 2;
             }
             else
             {
                 cout << "Same size. Running binary comparison...";
                 // Left off here
                 return 0;
             }
         }
    I compiled it in visual and it work but not sure if it was a dll
    here it is if u still want it

Similar Threads

  1. For the first time in almost 2 years I got this
    By SmartAlley in forum General
    Replies: 25
    Last Post: 06-24-2011, 07:17 PM
  2. [Help] [Unpacking Cshell.dll] I got one error !!
    By oiy in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 2
    Last Post: 12-02-2010, 01:44 AM
  3. first time 2 play combat arms.. 2bad i got error!!
    By JanRon in forum Combat Arms Help
    Replies: 4
    Last Post: 11-24-2010, 01:25 PM
  4. First Time It May Be a Bit Painful
    By Gourav2122 in forum General
    Replies: 11
    Last Post: 08-19-2007, 08:04 PM
  5. I need one of these!
    By sf0d in forum General
    Replies: 5
    Last Post: 05-16-2007, 04:13 PM

Tags for this Thread