文件 C

发现fprintf(fp,"%6d",,x);

与printf("%6d",x);

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");
 }
}

在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;这样问题也可得到解决。

posted @ 2013-06-12 16:32  herizai  阅读(183)  评论(0编辑  收藏  举报