介绍文件操作相关的函数

文件句柄 FILE*,FILE是个结构体,放的是文件的属性,结构体由fopen创建

#include<stdio.h>
int main
{
int a=10000;
FILE*pf=fopen("test.txt","wb");
fwrite(&a,4,1,pf);//二进制的形式写入到文件
fclose(pf);
pf=NULL;
return 0;
}

运行时程序默认打开三个文件:键盘,显示器,显示器。
stdin stdout stderr
标准输入 标准输出 标准错误

fopen fclose

FILE * fopen ( const char * filename, const char * mode );
int fclose ( FILE * stream );

#include <stdio.h>
 int main ()
  {  
  FILE * pFile; 
   pFile = fopen ("myfile.txt","w");
     if (pFile!=NULL)
       {  
         fputs ("fopen example",pFile);  
           fclose (pFile); 
            } 
             return 0; 
             } 

int fseek ( FILE * stream, long int offset, int origin );

#include <stdio.h>
 
int main ()
 {
   FILE * pFile; 
    pFile = fopen ( "example.txt" , "wb" );  
    fputs ( "This is an apple." , pFile ); 
     fseek ( pFile , 9 , SEEK_SET ); 
      fputs ( " sam" , pFile );
        fclose ( pFile );  
        return 0;
        }

ftell
返回文件指针相对于起始位置的偏移量
long int ftell ( FILE * stream );

#include <stdio.h>
 
int main ()
 { 
  FILE * pFile;  
  long size;
 
  pFile = fopen ("myfile.txt","rb"); 
   if (pFile==NULL) 
   perror ("Error opening file");  
   else 
    {  
      fseek (pFile, 0, SEEK_END);  
          size=ftell (pFile);  
            fclose (pFile);  
              printf ("Size of myfile.txt: %ld bytes.\n",size); 
               } 
                return 0;
                 }
                 

rewind

让文件指针的位置回到文件的起始位置
void rewind ( FILE * stream );

#include <stdio.h>
int main () 
{
  int n; 
   FILE * pFile;  
   char buffer [27];
  pFile = fopen ("myfile.txt","w+");
    for ( n='A' ; n<='Z' ; n++)  
      fputc ( n, pFile);  
      rewind (pFile);  
      fread (buffer,1,26,pFile); 
       fclose (pFile);
         buffer[26]='\0';  
         puts (buffer); 
          return 0; 
          }

功能 函数名 适用于
字符输入函数 fgetc 所有输入流
字符输出函数 fputc 所有输出流
文本行输入函数 fgets 所有输入流
文本行输出函数 fputs 所有输出流
格式化输入函数 fscanf 所有输入流
格式化输出函数 fprintf 所有输出流
二进制输入 fread 文件
二进制输出 fwrite 文件

##   二进制文件的例子
#include<stsdio.h>
enum { SIZE = 5 };
 int main(void)
  {   
   double a[SIZE] = {1.0,2.0,3.0,4.0,5.0};   
    double b = 0.0;  
      size_t ret_code = 0; 
         FILE *fp = fopen("test.bin", "wb"); // 必须用二进制模式   
          fwrite(a, sizeof(*a), SIZE, fp); // 写 double 的数组  
            fclose(fp);
    fp = fopen("test.bin","rb");    // 读 double 的数组  
      while((ret_code = fread(&b, sizeof(double), 1, fp))>=1)   
       {      
         printf("%lf\n",b);   
          }   
           if (feof(fp))    
               printf("Error reading test.bin: unexpected end of file\n");    
               else if (ferror(fp))
                {     
                   perror("Error reading test.bin");    
                   } 
                      fclose(fp);   
                       fp = NULL;
                       }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值