Created
March 2, 2013 17:58
-
-
Save xymostech/5072238 to your computer and use it in GitHub Desktop.
Working examples for http://stackoverflow.com/questions/15177218
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <cstdlib> | |
#include <cstring> | |
int main(int argc, char *argv[]) { | |
(void)argc; | |
bool good = true; | |
if (argc < 4) { | |
std::cout << "Wrong number of args: <size> <file1> <file2>" << std::endl; | |
return 1; | |
} | |
int buff_size = atoi(argv[1]); | |
int count = 0; | |
char buff_1[buff_size]; | |
char buff_2[buff_size]; | |
std::ifstream file1(argv[2], std::ios::in | std::ios::binary); | |
std::ifstream file2(argv[3], std::ios::in | std::ios::binary); | |
if (!file1.good() || !file2.good()) { | |
std::cout << "Error opening files" << std::endl; | |
return 1; | |
} | |
std::streamsize read1 = 0, read2 = 0; | |
do { | |
count++; | |
file1.read(buff_1, buff_size); | |
file2.read(buff_2, buff_size); | |
read1 = file1.gcount(); | |
read2 = file1.gcount(); | |
if (read1 != read2 || std::memcmp(buff_1, buff_2, read1) != 0) { | |
good = false; | |
break; | |
} | |
} while(file1.good() && file2.good()); | |
if (good) { | |
std::cout << "Same, " << count << std::endl; | |
} else { | |
std::cout << "Different, " << count << std::endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
public class Test { | |
public static void main(String[] args) throws FileNotFoundException, IOException { | |
boolean result = true; | |
int count = 0; | |
int size = Integer.parseInt(args[0]); | |
InputStream is1 = new BufferedInputStream(new FileInputStream(args[1])); | |
InputStream is2 = new BufferedInputStream(new FileInputStream(args[2])); | |
byte[] buffer1 = new byte[size]; | |
byte[] buffer2 = new byte[size]; | |
int readBytesCount1 = 0, readBytesCount2 = 0; | |
while ( | |
(readBytesCount1 = is1.read(buffer1)) != -1 && | |
(readBytesCount2 = is2.read(buffer2)) != -1 | |
) { | |
if (Arrays.equals(buffer1, buffer2) && readBytesCount1 == readBytesCount2) { | |
count++; | |
} else { | |
result = false; | |
break; | |
} | |
} | |
if (result) { | |
System.out.println("Same, " + count); | |
} else { | |
System.out.println("Different, " + count); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment