C-文件处理-随机访问文件
时间:2020-02-23 14:31:54 来源:igfitidea点击:
在本教程中,我们将学习使用C编程语言随机访问文件数据。
在本教程中,我们将讨论fseek(),ftell()和rewind()函数以访问文件中的数据。
ftell函数
ftell()函数告诉我们文件中的当前位置(以字节为单位)。
语法:
pos = ftell(fptr);
其中," fptr"是文件指针。
pos保持当前位置,即读取(或者写入)的总字节数。
例:
如果一个文件有10个字节的数据,并且ftell()函数返回4,则意味着已经读取(或者写入)了4个字节。
rewind函数
我们使用rewind()函数返回文件的起点。
语法:
rewind(fptr);
其中," fptr"是文件指针。
fseek函数
我们使用fseek()函数将文件位置移动到所需位置。
语法:
fseek(fptr, offset, position);
其中," fptr"是文件指针。
类型为long的偏移量,指定要从位置指定的位置在文件中移动的位置数(以字节为单位)。
" position"可以采用以下值。
- 0-文件的开头
- 1-文件中的当前位置
- 2-文件结尾
以下是我们可以使用fseek()函数执行的操作列表。
| 操作 | 说明 |
|---|---|
fseek(fptr,0,0) | 这会将我们带到文件的开头。 |
fseek(fptr,0,2) | 这将带我们到文件末尾。 |
fseek(fptr,N,0) | 将带我们到文件中第(N +1)个字节。 |
fseek(fptr,N,1) | 这将使我们从文件中的当前位置开始向前N个字节。 |
fseek(fptr,-N,1) | 这将使我们从文件中的当前位置向后N个字节。 |
fseek(fptr,-N,2) | 这将使我们从文件的末尾向后N个字节。 |
用C编写一个程序以将字母A保存到文件Z,然后使用fseek函数打印字母
#include <stdio.h>
int main(void) {
//integer variable
int i;
//character variable
char ch;
//file pointer
FILE *fptr;
//open file in write mode
fptr = fopen("char", "w");
if (fptr != NULL) {
printf("File created successfully!\n");
}
else {
printf("Failed to create the file.\n");
//exit status for OS that an error occured
return -1;
}
//write data in file
for (ch = 'A'; ch <= 'Z'; ch++) {
putc(ch, fptr);
}
//close connection
fclose(fptr);
//reference
printf("\nReference:\n");
for (i = 0; i < 26; i++) {
printf("%d ", (i+1));
}
printf("\n");
for (i = 65; i <= 90; i++) {
//print character
printf("%c", (i));
//manage space
if (i - 65 >= 9) {
printf(" ");
}
else {
printf(" ");
}
}
printf("\n\n");
//open file for reading
fptr = fopen("char", "r");
printf("Curr pos: %ld\n", ftell(fptr));
//read 1st char in the file
fseek(fptr, 0, 0);
ch = getc(fptr);
printf("1st char: %c\n", ch);
printf("Curr pos: %ld\n", ftell(fptr));
//read 5th char in the file
fseek(fptr, 4, 0);
ch = getc(fptr);
printf("5th char: %c\n", ch);
printf("Curr pos: %ld\n", ftell(fptr));
//read 26th char in the file
fseek(fptr, 25, 0);
ch = getc(fptr);
printf("26th char: %c\n", ch);
printf("Curr pos: %ld\n", ftell(fptr));
//rewind
printf("rewind\n");
rewind(fptr);
printf("Curr pos: %ld\n", ftell(fptr));
//read 10th char in the file
fseek(fptr, 9, 0);
ch = getc(fptr);
printf("10th char: %c\n", ch);
printf("Curr pos: %ld\n", ftell(fptr));
//read 15th char in the file
fseek(fptr, 4, 1); //move 4 bytes forward from current position
ch = getc(fptr);
printf("15th char: %c\n", ch);
printf("Curr pos: %ld\n", ftell(fptr));
//read 20th char in the file
fseek(fptr, 4, 1); //move 4 bytes forward from current position
ch = getc(fptr);
printf("20th char: %c\n", ch);
printf("Curr pos: %ld\n", ftell(fptr));
//close connection
fclose(fptr);
return 0;
}
File created successfully! Reference: 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 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Curr pos: 0 1st char: A Curr pos: 1 5th char: E Curr pos: 5 26th char: Z Curr pos: 26 rewind Curr pos: 0 10th char: J Curr pos: 10 15th char: O Curr pos: 15 20th char: T Curr pos: 20

