20121011总结——文件操作函数:rewind(fp) - fseek(fp, offset, seek_set) - ftell(fp)
2013-09-18 21:12
579 查看
说明:
rewind () 函数,
fseek (fp, offset, seek_set) 函数,
ftell (fp) 函数。
程序附录:
/* Description: the test of function
* rewind()
* fseek(fp, offset, seek_set)
* ftell(fp)
* Time: 2012.10.11
* Author: essencelite
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
FILE * fp;
char *ps = "fseek(), fsetpos() return 0, and ftell() returns the current offset. \
Otherwise, -1 is returned and errno is set to indicate the error.";
int length = strlen(ps);
/* creat re.txt and copy content to re.txt */
fp = fopen("re.txt", "w");
if (fp == NULL) {
perror("fopen re.txt error\n");
exit(EXIT_FAILURE);
}
int ret = fwrite(ps, 1, length, fp);
printf("ret = %d \n",ret);
fclose(fp);
/*---------------------------------------rewind()-------------------------------------------
* description: set the fp to the start of the file
*-----------------------------------------------------------------------------------------*/
/* test the rewind() function */
int i = 0;
char ch = 0;
fp = fopen("re.txt", "r");
if (fp == NULL) {
perror("fopen error\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < 10; i++) {
ch = fgetc(fp);
printf("_%c_", ch);
}
printf("\n");
rewind(fp);
ch = fgetc(fp);
printf("ch = %c \n", ch);
fclose(fp);
/*--------------------------------fseek(fp, offset, seek_set)-------------------------------
* description: set the fp to point to the station (seek_set+offset);
* from "seek_set" to "seek_set + offset", fp point to the (seek_set+offset);
* example: fseek(fp, 10L, 8) which means:from 8 to 18; fp point to the 18;
* the offset can be negative;
* parameters: set = 0, stand the start of file;
* cur, stand current station;
* END, stand end of the file;
*-----------------------------------------------------------------------------------------*/
/* test the fseek(fp, offset, seek_set) function */
i = 0;
ch = 0;
fp = fopen("re.txt", "r");
if (fp == NULL) {
perror("fopen error\n");
exit(EXIT_FAILURE);
}
fseek(fp, 10L, 0);
/* -------------------------------------ftell(fp)------------------------------------------
*description: location the current station of the fp
*---------------------------------------------------------------------------------------*/
/* test the ftell() function */
long tell = ftell(fp);
printf("after 10 offset form ,the current fp station, tell = %d \n", tell);
printf("\n");
fclose(fp);
return 0;
}
rewind () 函数,
fseek (fp, offset, seek_set) 函数,
ftell (fp) 函数。
程序附录:
/* Description: the test of function
* rewind()
* fseek(fp, offset, seek_set)
* ftell(fp)
* Time: 2012.10.11
* Author: essencelite
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
FILE * fp;
char *ps = "fseek(), fsetpos() return 0, and ftell() returns the current offset. \
Otherwise, -1 is returned and errno is set to indicate the error.";
int length = strlen(ps);
/* creat re.txt and copy content to re.txt */
fp = fopen("re.txt", "w");
if (fp == NULL) {
perror("fopen re.txt error\n");
exit(EXIT_FAILURE);
}
int ret = fwrite(ps, 1, length, fp);
printf("ret = %d \n",ret);
fclose(fp);
/*---------------------------------------rewind()-------------------------------------------
* description: set the fp to the start of the file
*-----------------------------------------------------------------------------------------*/
/* test the rewind() function */
int i = 0;
char ch = 0;
fp = fopen("re.txt", "r");
if (fp == NULL) {
perror("fopen error\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < 10; i++) {
ch = fgetc(fp);
printf("_%c_", ch);
}
printf("\n");
rewind(fp);
ch = fgetc(fp);
printf("ch = %c \n", ch);
fclose(fp);
/*--------------------------------fseek(fp, offset, seek_set)-------------------------------
* description: set the fp to point to the station (seek_set+offset);
* from "seek_set" to "seek_set + offset", fp point to the (seek_set+offset);
* example: fseek(fp, 10L, 8) which means:from 8 to 18; fp point to the 18;
* the offset can be negative;
* parameters: set = 0, stand the start of file;
* cur, stand current station;
* END, stand end of the file;
*-----------------------------------------------------------------------------------------*/
/* test the fseek(fp, offset, seek_set) function */
i = 0;
ch = 0;
fp = fopen("re.txt", "r");
if (fp == NULL) {
perror("fopen error\n");
exit(EXIT_FAILURE);
}
fseek(fp, 10L, 0);
/* -------------------------------------ftell(fp)------------------------------------------
*description: location the current station of the fp
*---------------------------------------------------------------------------------------*/
/* test the ftell() function */
long tell = ftell(fp);
printf("after 10 offset form ,the current fp station, tell = %d \n", tell);
printf("\n");
fclose(fp);
return 0;
}
相关文章推荐
- C语言 文件操作11--文件函数再讲 fseek()和ftell()
- PHP移动文件指针ftell()、fseek()、rewind()函数总结
- c文件操作之fopen、fclose、fread、fwrite及相关fseek、ftell、rewind例子
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- PHP移动文件指针ftell()、fseek()、rewind()函数总结
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- PHP移动文件指针ftell()、fseek()、rewind()函数总结
- 基本文件操作&fopen函数&FILE指针&ftell &fseek&rewind&fgets文件复制&feof
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- C++笔记 | C fopen函数 fwrite函数 rewind fseek fscanf fprintf等文件操作函数
- C文件操作——fopen/fseek/ftell/fread/fwrite/fclose等函数用法
- PHP移动文件指针ftell()、fseek()、rewind()函数总结
- 总结文件操作函数(一)-C语言
- nodejs文件操作模块FS(File System)常用函数简明总结
- 文件操作之fseek,ftell
- 【转】有趣儿的PHP文件操作常用函数总结
- 文件操作函数总结
- PHP文件读写操作相关函数总结
- 让你提前认识软件开发(18):C语言中常用的文件操作函数总结及使用方法演示代码
- C语言文件操作函数总结