c-language file operations


In c-language file operation,files include text files and binaries.Both of them exist as streams in system.

Three important streams about terminal.

When a file is written, the written data is stored in the cache.

1.stdin (standard input streams)

2.stdout (standard output streams)

3.stderr (standard error streams)

Open files.

function prototype :
FILE *fopen(const char *path, const char *mode);
parameter:
The string “path” contains path and filename of the file to open.The string “mode” represents the type of stream to open.

Some tips of parameter “mode” :

  • With ‘+’ for read/write, read/write is allowed.
  • Contains ‘b’ for open binary file, otherwise for text file.
  • ‘r’, read only open;If and only if it contains ‘r’, the file must exist, otherwise you can create a new file.
  • ‘w’, write, if there is a file, then delete and then new, equivalent to write from scratch.
  • ‘a’, append, write at the end.
    eg:
    *FILE fp=fopen(“file.txt”,“r”);

Close files.

function prototype :
int fclose(FILE *stream);
eg:
fclos(fp);
After manipulating the file,remeber to close it.

the read and write operations of files.

“fgets” and “fputs”

fgets

Function “fgets” is used to read data from files.
function prototype :
char *fgets(char *s, int size, FILE *stream);

fputs

Function “fputs” is used to write data to files.
function prototype :
int fputs(const char *s, FILE *stream);

“fscanf” and “fprintf”

fscanf

It is used to formatted read.
function prototype :
int fscanf(FILE *stream, const char *format, …);

fprintf

It is used to formatted write.
function prototype :
int fprintf(FILE *stream, const char *format, …);

“fread” and “fwrite”

fread

It is used to read data from a binary file.
function prototype :
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

fwrite

The data to be written to a file is written to the file in binary form.
function prototype :
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值