fopen等文件读写函数

fopen() 函数:

FILE *fopen(const char *filename, const char *mode)

https://www.runoob.com/cprogramming/c-function-fopen.html

fseek() 函数:

int fseek(FILE *stream, long int offset, int whence)

https://www.runoob.com/cprogramming/c-function-fseek.html

ftell() 函数:

long int ftell(FILE *stream)

https://www.runoob.com/cprogramming/c-function-ftell.html

fread() 函数:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)

https://www.runoob.com/cprogramming/c-function-fread.html

fwrite() 函数:

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)

https://www.runoob.com/cprogramming/c-function-fwrite.html

fclose() 函数:

int fclose(FILE *stream)

https://www.runoob.com/cprogramming/c-function-fclose.html

C 标准库 - <stdio.h>
https://www.runoob.com/cprogramming/c-standard-library-stdio-h.html

获取文件大小:

#include <stdio.h>

int main ()
{
   FILE *fp;
   int len;
   char str[] = "This is runoob.com";
 
   fp = fopen( "file.txt" , "w" );
   fwrite(str, sizeof(str) , 1, fp );//注意sizeof(str)是19个字节
   fclose(fp);

   fp = fopen("file.txt", "r");	//前提该文件必须存在
   if( fp == NULL ) 
   {
      perror ("打开文件错误");
      return(-1);
   }
   fseek(fp, 0, SEEK_END);

   len = ftell(fp);
   fclose(fp);

   printf("file.txt 的总大小 = %d 字节\n", len);
   
   return(0);
}

读取文件内容:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
   FILE *fp;
   int len;
   char *pbuf = NULL;
   char str[] = "This is runoob.com";
 
   fp = fopen( "file.txt" , "w" );
   fwrite(str, sizeof(str) , 1, fp );//注意sizeof(str)是19个字节
   fclose(fp);

   fp = fopen("file.txt", "r");	//前提该文件必须存在
   if( fp == NULL ) 
   {
      perror ("打开文件错误");
      return(-1);
   }
   fseek(fp, 0, SEEK_END);

   len = ftell(fp);
   fseek(fp, 0, SEEK_SET);
   
   //申请内存
   pbuf= (char *) malloc(len+1);
   memset(pbuf,0,len+1);
   /* 读取并显示数据 */
   fread(pbuf, len, 1, fp);
   printf("show: %s.\n", pbuf);

   fclose(fp);

   printf("file.txt 的总大小 = %d 字节\n", len);
   
   return(0);
}
  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`fopen_s` 函数是一个用于文件的 C 标准库函数,它提供了一种更加安全的方式来打开文件。 我们知道,通常在 C 语言中,可以使用 `fopen` 函数来打开文件并返回一个文件指针,以便进行后续的文件读写操作。但是,`fopen` 函数存在一些安全隐患,例如在一些情况下可能无法处理文件名超长、无法处理文件打开失败等问题。 为了解决这些问题,C11 标准引入了 `fopen_s` 函数作为 `fopen` 函数的替代品。`fopen_s` 函数的原型如下: ```c errno_t fopen_s(FILE** pFile, const char* filename, const char* mode) ``` 其中,`pFile` 是一个指针的指针,用于存储打开的文件指针;`filename` 是要打开的文件名;`mode` 是打开文件的模式,和 `fopen` 函数的模式参数一致。 与 `fopen` 函数不同的是,`fopen_s` 函数在打开文件时需要传入 `pFile` 参数,可以更好地处理文件打开失败的情况。如果文件打开成功,`fopen_s` 函数返回 0,否则返回一个错误码。 使用 `fopen_s` 函数打开文件的示例代码如下所示: ```c #include <stdio.h> int main() { FILE* pFile; errno_t err; err = fopen_s(&pFile, "example.txt", "r"); if (err != 0) { printf("无法打开文件\n"); return 1; } // 文件操作 fclose(pFile); return 0; } ``` 在上述代码中,我们首先定义了一个 `FILE` 类型的指针 `pFile` 来存储打开的文件指针。然后,我们调用 `fopen_s` 函数来打开文件,如果函数返回值不为 0,则表示打开文件失败。 最后,我们可以进行文件写操作,并在文件使用完毕后调用 `fclose` 函数关闭文件。 综上所述,`fopen_s` 函数是一个更安全的文件打开函数,可以更好地处理文件打开失败的情况,并通过错误码返回错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值