Reading
| 26 | |
| 27 | // Reading |
| 28 | void test_reading() { |
| 29 | FILE *file = fopen("somefile.binary", "rb"); |
| 30 | assert(file); |
| 31 | |
| 32 | fseek(file, 0, SEEK_END); |
| 33 | int size = ftell(file); |
| 34 | rewind (file); |
| 35 | printf("size: %d\n", size); |
| 36 | |
| 37 | char *buffer = (char*)malloc(sizeof(char)*size); |
| 38 | assert(buffer); |
| 39 | |
| 40 | size_t read = fread(buffer, 1, size, file); |
| 41 | assert(read == size); |
| 42 | |
| 43 | printf("data: %d", buffer[0]); |
| 44 | for (int i = 1; i < size; i++) |
| 45 | printf(",%d", buffer[i]); |
| 46 | printf("\n"); |
| 47 | |
| 48 | fclose(file); |
| 49 | free(buffer); |
| 50 | |
| 51 | // Do it again, with a loop on feof |
| 52 | printf("loop: "); |
| 53 | file = fopen("somefile.binary", "rb"); |
| 54 | assert(file); |
| 55 | while (!feof(file)) { |
| 56 | char c = fgetc(file); |
| 57 | if (c != EOF) printf("%d ", c); |
| 58 | } |
| 59 | fclose(file); |
| 60 | printf("\n"); |
| 61 | } |
| 62 | |
| 63 | // Standard streams |
| 64 | void test_stdstreams() { |
no test coverage detected