Created
October 12, 2019 17:16
-
-
Save vasalf/326a9588f2ef2f963faaa9930695f8dc to your computer and use it in GitHub Desktop.
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char* argv[]) { | |
printf("argc: %d\n", argc); | |
for (int i = 0; i < argc; i++) { | |
printf("argv[%d]: %s\n", i, argv[i]); | |
} | |
printf("\n"); | |
if (argc < 3) { | |
printf("At least two arguments must be provided\n"); | |
return 2; | |
} else if (strcmp(argv[1], "print") == 0) { | |
printf("%s\n", argv[2]); | |
} else if (strcmp(argv[1], "sum") == 0) { | |
if (argc < 4) { | |
printf("sum: at least two arguments must be provided\n"); | |
return 2; | |
} | |
int x, y; | |
if (sscanf(argv[2], "%d", &x) != 1) { | |
printf("sum: %s: not a number\n", argv[2]); | |
return 2; | |
} | |
if (sscanf(argv[3], "%d", &y) != 1) { | |
printf("sum: %s: not a number\n", argv[3]); | |
return 2; | |
} | |
printf("%d\n", x + y); | |
} else { | |
printf("Unknown command: %s\n", argv[1]); | |
return 2; | |
} | |
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
#include <stdio.h> | |
#include <string.h> | |
const void* min(const void* a, const void* b, int (*compare)(const void*, const void*)) { | |
if (compare(a, b) < 0) { | |
return a; | |
} else { | |
return b; | |
} | |
} | |
int int_compare(const void* x, const void* y) { | |
if (*(const int*)x < *(const int*)y) | |
return -1; | |
else if (*(const int*)x == *(const int*)y) | |
return 0; | |
return 1; | |
} | |
int string_compare(const void* x, const void* y) { | |
return strcmp((const char*)x, (const char*)y); | |
} | |
int main() { | |
int x = 239, y = 179; | |
printf("%d\n", *(const int*)min(&x, &y, int_compare)); | |
const char* a = "abac"; | |
const char* b = "abacaba"; | |
printf("%s\n", (const char*)min(a, b, string_compare)); | |
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
#include <stdio.h> | |
#include <stddef.h> | |
int main() { | |
int retval = 0; | |
FILE* in = fopen("input.txt", "r"); | |
if (in == NULL || ferror(in)) { | |
printf("Failed to open input file\n"); | |
retval = 1; | |
goto ret; | |
} | |
FILE* out = fopen("output.txt", "w"); | |
if (out == NULL || ferror(out)) { | |
printf("Failed to open output file\n"); | |
retval = 1; | |
goto close_in; | |
} | |
int x; | |
while (fscanf(in, "%d", &x) == 1) { | |
fprintf(out, "%d\n", x + 1); | |
} | |
if (!feof(in)) { | |
printf("File format error\n"); | |
retval = 1; | |
goto close_out; | |
} | |
close_out: | |
fclose(out); | |
close_in: | |
fclose(in); | |
ret: | |
return retval; | |
} |
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 <stdio.h> | |
#include <stddef.h> | |
int main() { | |
int retval = 0; | |
FILE* in = fopen("input.bin", "rb"); | |
if (in == NULL || ferror(in)) { | |
printf("Failed to open input file\n"); | |
retval = 1; | |
goto ret; | |
} | |
FILE* out = fopen("output.bin", "wb"); | |
if (out == NULL || ferror(out)) { | |
printf("Failed to open output file\n"); | |
retval = 1; | |
goto close_in; | |
} | |
unsigned char buf[5]; | |
size_t read; | |
while ((read = fread(buf, 1, 5, in)) > 0) { | |
for (int i = 0; i < 5; i++) | |
buf[i]++; | |
if (fwrite(buf, 1, read, out) != read) { | |
printf("File write error\n"); | |
retval = 1; | |
goto close_out; | |
} | |
} | |
if (ferror(in)) { | |
printf("File read error\n"); | |
retval = 1; | |
goto close_out; | |
} | |
close_out: | |
fclose(out); | |
close_in: | |
fclose(in); | |
ret: | |
return retval; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment