-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
64 lines (52 loc) · 1.5 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* read_file(char *filename)
{
char* buffer = NULL;
int string_size, read_size;
FILE* fptr = fopen(filename, "r");
if (fptr){
// Set filesize seeking for last byte of the file
fseek(fptr, 0, SEEK_END);
string_size = ftell(fptr);
// go back to the start of the file
rewind(fptr);
// Allocate memory
buffer = (char*) malloc(sizeof(char) * (string_size + 1) );
// Read file
read_size = fread(buffer, sizeof(char), string_size, fptr);
// Put a \0 in the last position so that buffer is a string
buffer[string_size] = '\0';
if (string_size != read_size){
// If something goes wrong, free memory and set the buffer to NULL
free(buffer);
buffer = NULL;
}
fclose(fptr);
}
else{
printf("File %s not open correctly\n", filename);
}
return buffer;
}
int main(int argc, char *argv[]){
char* filename1 = "cuda.txt";
char* filename2 = "omp.txt";
if(argc>1){
filename1 = argv[1];
filename2 = argv[2];
}
char* file1 = read_file(filename1);
char* file2 = read_file(filename2);
if (file1 && file2)
{
char* equal = strcmp(file1, file2) == 0? "" : "not ";
printf("Files %s and %s are %sequal\n", filename1, filename2, equal);
}
else{
printf("Files not read correctly\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}