-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.c
67 lines (52 loc) · 1.54 KB
/
reader.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
65
66
67
#include <stdio.h> // for printf, and file functions
#include <stdlib.h> // for system()
// OS check
#ifdef _WIN32
#define CLEAR "cls" // Windows
#elif __unix__
#define CLEAR "clear" // Unix-Like (Linux, BSD, Etc)
#elif __APPLE__
#define CLEAR "clear" // MacOS
#else
#define OSNOTSUPPORTED // if OS not suported
#endif
// Functions Prototypes
void ReadAndPrintFile(const char *FileName);
// Main Function
int main(int argc, char *argv[]){
// in case of OS Not Supported
#ifdef OSNOTSUPPORTED
printf("OS not supported\n\n");
return 2;
#endif
// Arguments Check
if (argc <= 1){
printf("Missing Arguments (File Path)\n");
return 1;
} else if (argc > 2){
printf("Too many arguments\n");
return 1;
}
// Saving File Name
const char *nFile = argv[1];
system(CLEAR);
ReadAndPrintFile(nFile);
// Return 0 and Closing Main Function (Exit Program)
return 0;
}
void ReadAndPrintFile(const char *FileName){
FILE *pFile = fopen(FileName, "r");
if (!pFile){ // Error if file dont exist or cannot read
printf("Error Opening the File\n");
return;
}
do { // Read the file in a loop until it is finished
int buffer = fgetc(pFile);
if (buffer != EOF){ // Continue reading if you haven't reached the end of the file
printf("%c", (char) buffer);
} else {
printf("\n");
}
} while (!feof(pFile));
fclose(pFile);
}