与文件操作相关的函数

简介:

一.fopen函数:

函数原型:FILE*   fopen(const   char *  path,   const   char*  mode)

path参数 :表示一个文件名,可以包含路径和文件名。如:“test.txt”  或"C:\\homework\\test.txt"  注意有两个\\,因为其中一个  ’\‘  代表转义字符。

mode参数: man fopen文本中的内容:

 r      Open text file for reading.  The stream  is  positioned  at  the
              beginning of the file.

       r+     Open  for  reading and writing.  The stream is positioned at the
              beginning of the file.

       w      Truncate file to zero length or create text  file  for  writing.
              The stream is positioned at the beginning of the file.

       w+     Open  for  reading  and writing.  The file is created if it does
              not exist, otherwise it is truncated.  The stream is  positioned
              at the beginning of the file.

       a      Open  for  appending (writing at end of file).  The file is cre‐
              ated if it does not exist.  The stream is positioned at the  end
              of the file.

       a+     Open  for  reading  and appending (writing at end of file).  The
              file is created if it does not exist.  The initial file position
              for  reading  is  at  the  beginning  of the file, but output is
              always appended to the end of the file.

注意:后三个为The file is created if it does  not exist, otherwise it is truncated

对文件的使用方式有如下6种:

文件使用方式由r,w,a,t,b,+六个字符拼成,各字符的含义是:
r(read): 读
w(write): 写
a(append): 追加
t(text): 文本文件,可省略不写
b(banary): 二进制文件
+: 读和写

返回值:如果成功,则返回一个文件指针,如果失败,则返回NULL。



二.fread函数:size_t   fread(void*  buf,  size_t   size,   size_t  nmemb,   FILE*   stream)

     fwrite函数:size_t   fwrite(const  void*  buf,   size_t   size,   size_t   nmemb,   FILE* stream)

buf参数:数据缓冲的地址,注意为void类型。

size参数: 因为buf为void类型,所以要指明每个数据项的大小。注意两个函数都是以数据项来读写的,一次读或写一个数据项,而不是字节。数据项可以是char,int,结构体等等。

nmemb参数:为希望读/写的数据项个数。注意,实际读/写操作不一定会读/写count各数据项。

stream参数:一个文件指针。

返回值:man  fread文本内容如下:

RETURN VALUE
       On  success,  fread()  and  fwrite() return the number of items read or
       written.  This number equals the number of bytes transferred only  when
       size  is 1.  If an error occurs, or the end of the file is reached, the
       return value is a short item count (or zero).

       fread() does not distinguish between end-of-file and error, and callers
       must use feof(3) and ferror(3) to determine which occurred.
文本总结:返回值都是实际读/写操作的数据项数,发生错误或者读到文件末尾并不是返回-1,也是一个读/写的数据项数或者0。如果要判断是发生错误还是到文件结尾,可以调用feof(3)和ferror(3)。


三.fclose函数:int   fclose(FILE*   fp)

关闭一个文件

返回值:返回值 若关文件动作成功则返回0,有错误发生时则返回EOF并把错误代码存到errno。



相关代码:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<string.h>
const int BUF_SIZE = 1024;

int main()
{
    FILE* stream;
    FILE* wstream;
    char buf[BUF_SIZE];

    stream = popen("ls -l","r");
    wstream = fopen("test_popen.txt","w+");

    memset(buf, '\0', sizeof(buf));
    fread(buf, sizeof(char), sizeof(buf), stream);//把返回标准I/O流内的内容读到buf中
    fwrite(buf,sizeof(char), strlen(buf), wstream);

    fclose(wstream);//一定要记得关闭
    pclose(stream);//只能用pclose不能用fclose

    return 0;
}

wKiom1cM7xyCyDaUAAB6di2I9d0677.png




四.fgets函数:char*  fgets(char*   buf,   size_t    size,    FILE*   stream)  ('\n'   ,   '\0')

       功能:从文件中读取一行,送到buf中。

      fgets参数:,参数s是缓冲区的首地址,size是缓冲区的长度,该函数从stream所指的文件中读取以'\n'结尾的一行(包括'\n'在内)存到缓冲区s中,并且在该行末尾添加一个'\0'组成完整的字符串

       返回值:  如果成功,buf指向哪返回值就指向哪,出差或读到文件尾时返回NULL。

注意:1.因为该函数会在行末尾加   ‘\0’  ,所以它最多只能读size-1个字符。

            2.因为该函数是读取一行,它的返回条件是读到   ‘\n’  ,它看待   '\0  '是一个普通的字符。

            3.它不能读二进制文件。



五.fputs函数: int   fputs(const  char *  buf,   FILE*    stream)     (没有'\0')

      功能:把buf中保存的字符串写到文件中。

      返回值:成功返回读入字符串数,出差返回EOF

注意:缓冲区s中保存的是以'\0'结尾的字符串,fputs将该字符串写入文件 stream,但并不写入结尾的'\0'。与fgets不同的是,fputs并不关心的字符串中的'\n'字符,


相关代码:

1
2
3
4
5
6
7
8
9
  1 #include<stdio.h>
  
   int  main()                                                                  
   4 {
   5      char  *buf =  "i want to have a star\n" ;
   6      FILE * fp =  fopen ( "./test.txt" , "w+" );
   7      fputs (buf,fp);
   8      return  0;
   9 }

wKiom1cPAOaBugMXAABErRslHNM471.png


六.fprintf函数:int  fprintf(FILE*  stream ,const  char *format,   . . .)
       功能:传送格式化输出到一个文件
       format 格式化输入函数,和printf里的格式一样
       返回值:成功时返回转换的字节数,失败时返回一个负数
       fp = fopen("./test.c","w+");
       fprintf(fp,"%s\n",str);


.fscnaf函数: int fscanf(FILE*  stream, const char*  format,   .  .  .)

       功能:从一个流中执行格式化输入
       format 格式化输出函数,和scanf里的格式一样 
       返回值:成功时返回转换的字节数,失败时返回一个负数
       fp = fopen("/local/test.c","a+");
       fscanf(fp,"%s",str);


八.fgetc函数:int fgetc(FILE* stream)
       函数说明: fgetc()从参数stream所指的文件中读取一个字符。若读到文件尾而无数据时便返回EOF。
       返回值: getc()会返回读取到的字符,若返回EOF则表示到了文件尾。

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
main()
{
FILE  *fp;
int  c;
fp= fopen (“./test.txt”,”w+”);
while ((c= fgetc (fp))!=EOF)
printf (“%c”,c);
fclose (fp);
}


九.fputc函数:int  fputc(int  c, FILE* stream)
       函数说明 :fputc 会将参数c 转为unsigned char 后写入参数stream 指定的文件中。
       返回值 :fputc()会返回写入成功的字符,即参数c。若返回EOF则代表写入失败。

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
main()
{
FILE  * fp;
char  a[26]=”abcdefghijklmnopqrstuvwxyz”;
int  i;
fp=  fopen (“./test.txt”,”w”);
for (i=0;i<26;i++)
fputc (a,fp);
fclose (fp);
}


十.flush函数:int  fflush(FILE*  stream)
函数说明 :fflush()会强迫将缓冲区内的数据写回参数stream指定的文件中。如果参数stream为NULL,fflush()会将所有打开的文件数据更新。
返回值 成功返回0,失败返回EOF,错误代码存于errno中。










本文转自 ye小灰灰  51CTO博客,原文链接:http://blog.51cto.com/10704527/1763701,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
移动开发 Linux 程序员
c++文件操作,超详细
c++文件操作,超详细
48 0
|
1月前
|
存储 程序员 编译器
|
1月前
|
C++
轻松学会文件操作(2)
轻松学会文件操作(2)
|
6月前
|
程序员 C语言 Windows
【文件操作】
【文件操作】
23 0
|
3月前
|
存储 C语言
文件操作及函数
文件操作及函数
39 0
|
4月前
|
存储 Java C语言
文件操作你会了吗
文件操作你会了吗
41 0
|
4月前
|
存储 程序员 C语言
文件操作详解
文件操作详解
30 0
|
5月前
|
存储 编译器 数据库
文件操作介绍(上)
文件操作介绍(上)
23 0
|
5月前
|
存储 C语言
文件操作介绍(下)
文件操作介绍(下)
28 0
|
7月前
|
C#
使用C#进行文件操作
在许多应用程序中,文件操作是常见的任务之一。无论是读取文件内容、写入数据,还是创建、移动和删除文件,C# 编程语言都提供了强大且易于使用的文件操作功能。本篇博客将介绍如何使用C#来进行基本的文件操作。
35 0