fseek fread()和fwrite()函数分析

来源:互联网 发布:java毫秒数转换成时间 编辑:程序博客网 时间:2024/04/25 14:46

c语言文件定位:fseek()函数和ftell()函数的使用
dev.firnow.com    时间 : 2010-06-28  作者:匿名   编辑:壹枝雪糕 点击:  1519 [ 评论 ]
-
-
fseek函数是用来设定文件的当前读写位置:

函数原型:int fseek(FILE *fp,long offset,int origin);
函数功能:把fp的文件读写位置指针移到指定的位置.

fseek(fp,20,SEEK_SET);

//意思是把fp文件读写位置指针从文件开始后移20个字节.

ftell函数是用来获取文件的当前读写位置;
函数原型: long ftell(FILE *fp)
函数功能:得到流式文件的当前读写位置,其返回值是当前读写位置偏离文件头部的字节数.

ban=ftell(fp);

//是获取fp指定的文件的当前读写位置,并将其值传给变量ban.

fseek函数与ftell函数综合应用:
分析:可以用fseek函数把位置指针移到文件尾,再用ftell函数获得这时位置指针距文件头的字节数,这个字节数就是文件的长度.

#include <stdio.h>
main()
{
   FILE *fp;
   char filename[80];
   long length;
   printf("Input the file name:");
   gets(filename);
   fp=fopen(filename,"rb");
   if(fp==NULL)
       printf("file not found!/n");
   else
   {
       fseek(fp,OL,SEEK_END);
       length=ftell(fp);
       printf("the file length %1d bytes/n",length);
       fclose(fp);
   }
}


在C语言中进行文件操作时,我们经常用到fread()和fwrite(),用它们来对文件进行读写操作。下面详细绍一下这两个函数的用法。

  我们在用C语言编写程序时,一般使用标准文件系统,即缓冲文件系统。系统在内存中为每个正在读写的文件开辟“文件缓冲区”,在对文件进行读写时数据都经过缓冲区。要对文件进行读写,系统首先开辟一块内存区来保存文件信息,保存这些信息用的是一个结构体,将这个结构体typedef为FILE类型。我们首先要定义一个指向这个结构体的指针,当程序打开一个文件时,我们获得指向FILE结构的指针,通过这个指针,我们就可以对文件进行操作。例如:

#i nclude <stdio.h>

#i nclude <string.h>

int main()

{

   FILE *fp;

   char buffer[100] = "This is a test";

   if((fp = fopen("c://example.txt", "w")) == 0)

    {

       printf("open failed!");

       exit(1);

    }

   fwrite(buffer, 1, strlen("This is a test"), fp);

   fclose(fp);

   return 0;

}

  通过以上代码,我们就在c盘的根目录下建立了一个名为example扩展名为.txt的文件,我们打开可以看到上面写上了This is a test。当我们对它将它读出时,用如下代码:

#i nclude <stdio.h>

#i nclude <mem.h>

int main()

{

   FILE *fp;   int len;

   char buffer[100];

   /*memset(buffer, 1, 100); */

   if((fp = fopen("c://example.txt", "r")) == 0)

    {

       printf("open failed!");

       exit(1);

    }

   fseek(fp, 0L, SEEK_END);

   len = ftell(fp);

   rewind(fp);

   fread(buffer, 1, len , fp);

   printf("%s",buffer);

   fclose(fp);

   getch();

   return 0;

}

 可以看到,当我们使用memset了以后,读出了一大堆乱码,这是为什么呢?原因是我们在fwrite函数时写入的字节数是用strlen求得的,也就是说字符串最后的'/0'并没有写到文件中去。所以我们从文件中读到buffer中时也自然没有'/0',因为buffer中的数是随机的,除非buffer中最后一个字符的下一个数恰好随机到0(可能性很小,这里用memset将它排除),否则以%s将buffer中的字符输出时遇不到0,所以乱码产生。解决的办法有很多,你可以在向文件写数据时多写入一个字节,系统会自动写入0,fwrite(buffer, 1, strlen("This is a test")+1, fp);这样读出时最后就有一个0了。或者读出操作完成后,在最后一个字符后面补上一个0:buffer[len] = 0;这样问题也可得到解决


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/greytree/archive/2006/06/02/769952.aspx

 

fread函数和fwrite函数

1.函数功能

  用来读写一个数据块。

2.一般调用形式

  fread(buffer,size,count,fp);

  fwrite(buffer,size,count,fp);

3.说明

  (1)buffer:是一个指针,对fread来说,它是读入数据的存放地址。对fwrite来说,是要输出数据的地址。

  (2)size:要读写的字节数;

  (3)count:要进行读写多少个size字节的数据项;

  (4)fp:文件型指针。

 注意:1 完成次写操(fwrite())作后必须关闭流(fclose());

           2 完成一次读操作(fread())后,如果没有关闭流(fclose()),则指针(FILE * fp)自动向后移动前一次读写的长度,不关闭流继续下一次读操作则接着上次的输出继续输出;

           3 fprintf() : 按格式输入到流,其原型是int fprintf(FILE *stream, const char *format[, argument, ...]);其用法和printf()相同,不过不是写到控制台,而是写到流罢了。注意的是返回值为此次操作写入到文件的字节数。如int c = fprintf(fp, "%s %s %d %f", str1,str2, a, b) ;str1:10字节;str2: 10字节;a:2字节;b:8字节,c为33,因为写入时不同的数据间自动加入一个空格。

文件使用之后一定要关闭,否则将不能正确显示内容.fwrite:读入两个学生信息然后用fwrite存入文件

fread:用fread从文件中读出学生信息。

 

fwrite.c

#include <stdio.h>
#define SIZE 2
struct student_type
{
 char name[10];
 int num;
 int age;
 char addr[10];
}stud[SIZE];
void save()
{
 FILE *fp;
 int i;
 if((fp=fopen("stu_list","wb"))==NULL)
 {
  printf("cant open the file");
  exit(0);
 }
 for(i=0;i<SIZE;i++)
 {
   if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
    printf("file write error/n");
 }
 fclose(fp);
}
main()
{
 int i;
 for(i=0;i<SIZE;i++)
 {
   scanf("%s%d%d%s",&stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
   save();
 }
 for(i=0;i<SIZE;i++)
 {
   printf("%s,%d,%d",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
 }
}

 

 

fread.c

#include <stdio.h>
#define SIZE 2
struct student_type
{
 char name[10];
 int num;
 int age;
 char addr[10];
}stud[SIZE];
void read()
{
 FILE *fp;
 int i;
 if((fp=fopen("stu_list","rb"))==NULL)
 {
  printf("cant open the file");
  exit(0);
 }
 for(i=0;i<SIZE;i++)
 {
   if(fread(&stud[i],sizeof(struct student_type),1,fp)!=1)
    printf("file write error/n");
 }
 fclose(fp);
}
main()
{

 int i;
 read();
 for(i=0;i<SIZE;i++)
 {
   printf("%s,%d,%d,%s",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
   printf("/n");
 }
}

 

原创粉丝点击