文件操作总结

 

File Operation Summary
                                          By xiaohuangyang at 2008-1-3
 
The FILE Poionter Structure:文件指针结构
 
#ifndef _FILE_DEFINED
struct _iobuf {
        char *_ptr;
        int   _cnt;
        char *_base;
        int   _flag;
        int   _file;
        int   _charbuf;
        int   _bufsiz;
        char *_tmpfname;
        };
typedef struct _iobuf FILE;
#define _FILE_DEFINED
#endif
 
1.Open A File 打开文件
Function :FILE * fopen(filename,accessmode);
Return value: if the fopen call is successful, a FILE pointer is returned to identify the file in the Subsequent I/O operations;otherwise, a null pointer is returned.
Notes: accessmode : “r”    read only
           “w” write only
                   “a”   appending the file
                   “r+” or “w+” read and write
                   “a+” read and write ,but all writes go to the end of the end of the file.
On systems that distinguish binary from text file ,the letter b must be append to the access mode(as in “rb”)to open a binary file.
 
If a file is not exist,it is created if the accessmode is “w”,w+”,”a”or”a+”;
 
 
2.Close a file 关闭文件
 Function : int fclose (filePtr)
 Return value : ,return 0 if the close is successful,or return EOF if an error occurs;
 
 
3.Get to a specific position of the file 到达文件的指定位置
 Function: int fseek(fileptr,offset,mode)
 Return value: if fseek is called successful ,a nozero value is returned;
 Notes: mode: “SEEK_SET” the offset is relative to the beginning of the file
                      “SEEK_END” the offset is relative to the end of the file
                      “SEEK_CUR” the offset is relative to the current position
offset is a long int. this function position the indicated file to a point that is offset
bytes From the beginning of the file /end of the file / current position.
 
 
4. Get the position of the file 得到当前的位置
   Function: long ftell(filePtr)
Return value: returns the relative offset in bytes of the current position in the file idendtified by filePtr,or -1L on error;
Notes: In windows operate system that distinguish binary from text file,
Windows 操作系统上,二进制文件和文本文件是区别对待的,对于文本文件,当向其内部写入数据时,一旦遇到换行(0x0a),则会转换为回车换行(0x0a ,0x0d)
当读取数据时,一旦遇到回车换行(0x0a,0x0d)组合,则会转化为换行(0x0a).
不管是二进制方式还是文本方式ftell(),fseek()函数是根据文件中实际的字符偏移为准的.
应当注意采用文本方式从文件开始处读取文件时,读取到的字符数和实际中该字符的偏移是不同的。因为回车换行两个字符在读取时只能读到一个字符。
 
Example:
//program 1
#include <stdio.h>
void main()
{
       FILE *fp = NULL;
 
       //open a.txt file in binary mode,create it first time .
       if( (fp = fopen("a.txt","wb")) != NULL)
       {
              fputc('A',fp);//A
              fputc('B',fp);//B
              fputc(0x0a,fp);
              printf(“%d”),.ftell(fp);
              fclose(fp);
       }
}
a.      txt内的内容
AB黑点C

Print output:4
//program 2
#include <stdio.h>
void main()
{
       FILE *fp = NULL;
 
       //open a.txt file in binary mode,create it first time .
       if( (fp = fopen("a.txt","w")) != NULL)
       {
              fputc('A',fp);//A
              fputc('B',fp);//B
              fputc(0x0a,fp);
printf(“%d”),.ftell(fp);
              fclose(fp);
       }
}
a.txt的内容:
AB
C
 
Print output:5
 
 
5.Read File or Write to File
 
//get one character
Function           : int fgetc(filePtr)
ReturnValue: return the next character of the file,or the value EOF if an end-of-file
                     Condition occurs;
//get a string less than a line.
Function:    char * fgets(buffer,i,filePtr);
ReturnValue: if an eof is reached or an error occurs ,the value NULL is returned ,otherwise return the buffer is returned
Notes:reads character from the inicated file ,untile i-1 characters are read or a new line character is read.
 
//get a formated data
Function: int fscanf( filePtr,format,arg1,arg2,…..argn)
ReturnValue: the number of items successfully read and assigned.or the value EOF if the end of file is reched before the first item is conveted.
Notes: Read data items from the the file identified by the filePtr,accord the format specified by the character string-- format.
 
//get N items data
Function: size_t fread( buffer,size,n,filePtr)
Returnvalue: the items successfully read.
Notes: Read n data items from the identified file into buffer.Each item of data is size bytes of length.
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值