78 lines
2.3 KiB
Plaintext
78 lines
2.3 KiB
Plaintext
|
|
int main() {
|
||
|
|
printf("=== File Seek Operations Tests ===\n");
|
||
|
|
|
||
|
|
printf("Test 1: Create test file\n");
|
||
|
|
int f = fopen("seek_test.txt", "w");
|
||
|
|
if (f == 0) {
|
||
|
|
printf("ERROR: Could not create file\n");
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
fputs(f, "0123456789ABCDEFGHIJ");
|
||
|
|
fclose(f);
|
||
|
|
printf("PASS: File created with content\n");
|
||
|
|
|
||
|
|
printf("Test 2: ftell() - get current position\n");
|
||
|
|
f = fopen("seek_test.txt", "r");
|
||
|
|
int pos = ftell(f);
|
||
|
|
printf("Initial position: %d\n", pos);
|
||
|
|
char *line = fgets(f, 5);
|
||
|
|
printf("Read: '%s'\n", line);
|
||
|
|
pos = ftell(f);
|
||
|
|
printf("Position after reading 5 bytes: %d\n", pos);
|
||
|
|
fclose(f);
|
||
|
|
printf("PASS: ftell() works\n");
|
||
|
|
|
||
|
|
printf("Test 3: fseek() with SEEK_SET\n");
|
||
|
|
f = fopen("seek_test.txt", "r");
|
||
|
|
fseek(f, 10, SEEK_SET());
|
||
|
|
pos = ftell(f);
|
||
|
|
printf("Position after fseek(10, SEEK_SET): %d\n", pos);
|
||
|
|
line = fgets(f, 5);
|
||
|
|
printf("Read from position 10: '%s'\n", line);
|
||
|
|
fclose(f);
|
||
|
|
printf("PASS: fseek with SEEK_SET works\n");
|
||
|
|
|
||
|
|
printf("Test 4: fseek() with SEEK_CUR\n");
|
||
|
|
f = fopen("seek_test.txt", "r");
|
||
|
|
line = fgets(f, 5);
|
||
|
|
printf("First read: '%s'\n", line);
|
||
|
|
fseek(f, 3, SEEK_CUR());
|
||
|
|
pos = ftell(f);
|
||
|
|
printf("Position after fseek(3, SEEK_CUR): %d\n", pos);
|
||
|
|
line = fgets(f, 5);
|
||
|
|
printf("Read after seek: '%s'\n", line);
|
||
|
|
fclose(f);
|
||
|
|
printf("PASS: fseek with SEEK_CUR works\n");
|
||
|
|
|
||
|
|
printf("Test 5: fseek() with SEEK_END\n");
|
||
|
|
f = fopen("seek_test.txt", "r");
|
||
|
|
fseek(f, -5, SEEK_END());
|
||
|
|
pos = ftell(f);
|
||
|
|
printf("Position after fseek(-5, SEEK_END): %d\n", pos);
|
||
|
|
line = fgets(f, 10);
|
||
|
|
printf("Read from near end: '%s'\n", line);
|
||
|
|
fclose(f);
|
||
|
|
printf("PASS: fseek with SEEK_END works\n");
|
||
|
|
|
||
|
|
printf("Test 6: Random access pattern\n");
|
||
|
|
f = fopen("seek_test.txt", "r");
|
||
|
|
fseek(f, 5, SEEK_SET());
|
||
|
|
line = fgets(f, 3);
|
||
|
|
printf("Position 5: '%s'\n", line);
|
||
|
|
fseek(f, 0, SEEK_SET());
|
||
|
|
line = fgets(f, 3);
|
||
|
|
printf("Position 0: '%s'\n", line);
|
||
|
|
fseek(f, 15, SEEK_SET());
|
||
|
|
line = fgets(f, 3);
|
||
|
|
printf("Position 15: '%s'\n", line);
|
||
|
|
fclose(f);
|
||
|
|
printf("PASS: Random access works\n");
|
||
|
|
|
||
|
|
printf("Test 7: Cleanup\n");
|
||
|
|
fremove("seek_test.txt");
|
||
|
|
printf("PASS: File removed\n");
|
||
|
|
|
||
|
|
printf("\n=== All File Seek Tests Completed ===\n");
|
||
|
|
return 0;
|
||
|
|
}
|