2011-01-17 00:52:25 +03:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-01-17 02:29:06 +03:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Reading
|
|
|
|
|
2011-01-17 00:52:25 +03:00
|
|
|
FILE *file = fopen("somefile.binary", "rb");
|
|
|
|
assert(file);
|
|
|
|
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
int size = ftell(file);
|
|
|
|
rewind (file);
|
|
|
|
printf("size: %d\n", size);
|
|
|
|
|
|
|
|
char *buffer = (char*) malloc (sizeof(char)*size);
|
|
|
|
assert(buffer);
|
|
|
|
|
|
|
|
size_t read = fread(buffer, 1, size, file);
|
|
|
|
assert(read == size);
|
|
|
|
|
|
|
|
printf("data: %d", buffer[0]);
|
|
|
|
for (int i = 1; i < size; i++)
|
|
|
|
printf(",%d", buffer[i]);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
fclose (file);
|
|
|
|
free (buffer);
|
|
|
|
|
2011-01-17 02:29:06 +03:00
|
|
|
// Standard streams
|
|
|
|
|
2011-06-19 23:03:10 +04:00
|
|
|
printf("input:%s\n", gets((char*)malloc(1024)));
|
2011-01-17 00:52:25 +03:00
|
|
|
fwrite("texto\n", 1, 6, stdout);
|
|
|
|
fwrite("texte\n", 1, 6, stderr);
|
2011-10-08 04:54:01 +04:00
|
|
|
putchar('$');
|
|
|
|
putc('\n', stdout);
|
2011-01-17 00:52:25 +03:00
|
|
|
|
2011-01-17 02:29:06 +03:00
|
|
|
// Writing
|
|
|
|
|
|
|
|
char data[5] = { 10, 30, 20, 11, 88 };
|
|
|
|
FILE *outf = fopen("go.out", "wb");
|
|
|
|
fwrite(data, 1, 5, outf);
|
|
|
|
fclose(outf);
|
|
|
|
|
|
|
|
char data2[10];
|
|
|
|
FILE *inf = fopen("go.out", "rb");
|
|
|
|
int num = fread(data2, 1, 10, inf);
|
|
|
|
fclose(inf);
|
|
|
|
printf("%d : %d,%d,%d,%d,%d\n", num, data2[0], data2[1], data2[2], data2[3], data2[4]);
|
|
|
|
|
2011-05-16 03:26:57 +04:00
|
|
|
// Test reading a file that has not been cached
|
|
|
|
|
|
|
|
FILE *other = fopen("test.file", "r");
|
|
|
|
assert(other);
|
2011-06-24 04:54:23 +04:00
|
|
|
|
2011-05-16 03:26:57 +04:00
|
|
|
char otherData[1000];
|
2011-06-24 02:38:30 +04:00
|
|
|
num = fread(otherData, 1, 9, other);
|
|
|
|
otherData[num] = 0;
|
2011-05-16 03:26:57 +04:00
|
|
|
printf("other=%s.\n", otherData);
|
|
|
|
|
2011-06-24 04:54:23 +04:00
|
|
|
// Seeking
|
|
|
|
|
|
|
|
fseek(other, 2, SEEK_SET);
|
|
|
|
num = fread(otherData, 1, 5, other);
|
|
|
|
otherData[num] = 0;
|
|
|
|
printf("seeked=%s.\n", otherData);
|
|
|
|
|
|
|
|
fseek(other, -1, SEEK_CUR);
|
|
|
|
num = fread(otherData, 1, 3, other);
|
|
|
|
otherData[num] = 0;
|
|
|
|
printf("seeked=%s.\n", otherData);
|
|
|
|
|
|
|
|
fseek(other, -2, SEEK_END);
|
|
|
|
num = fread(otherData, 1, 2, other);
|
|
|
|
otherData[num] = 0;
|
|
|
|
printf("seeked=%s.\n", otherData);
|
|
|
|
|
|
|
|
fclose(other);
|
|
|
|
|
2011-07-12 18:43:52 +04:00
|
|
|
// fscanf
|
|
|
|
|
|
|
|
outf = fopen("fscan.f", "w");
|
|
|
|
fprintf(outf, "10 hello");
|
|
|
|
fclose(outf);
|
|
|
|
|
|
|
|
int number;
|
|
|
|
char text[100];
|
|
|
|
inf = fopen("fscan.f", "r");
|
|
|
|
num = fscanf(inf, "%d %s", &number, text);
|
|
|
|
fclose(inf);
|
|
|
|
printf("fscanfed: %d - %s\n", number, text);
|
|
|
|
|
2011-01-17 00:52:25 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|