标准c的io操作

1. io操作实例

2. io函数分析

<1>. io 操作实例

1.1 文本文件读取

#include <stdio.h> // file io operators
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE* fp;
int ch;
long int count = 0;
if (argc != 2)
{
printf("usage : %s filename", argv[0]);
return EXIT_SUCCESS;
}
// open file
if ( (fp = fopen(argv[1], "r")) == NULL )
{
printf("can not open the file/n");
return EXIT_FAILURE;
}
while ( (ch = getc(fp)) != EOF )
{
printf("%c", ch);
count++;
}
// close file
fclose(fp);
// print how many chars in the file
printf("the file has %ld chars/n", count);
return EXIT_SUCCESS;
}

1.2 二进制文件读取

/*
* file_op.c
*
* Created on: Apr 19, 2011
* Author: xuqiang
*/
#include <stdio.h>
#include <stdlib.h>
#include "file_op.h"
void randbin()
{
const int NUMBER_SIZE = 1000;
double numbers[NUMBER_SIZE];
double read_numbers[NUMBER_SIZE];
const char* file = "number.dat";
long pos;
FILE* fp;
int index;
double value;
// make a random array
int i;
for (i = 0; i < NUMBER_SIZE; ++i)
{
numbers[i] = i;
}
// open file
if ( (fp = fopen(file, "wb")) == NULL )
{
printf("error to open the file for writing/n");
exit(1);
}
// write the data to file
fwrite(numbers, sizeof(double), NUMBER_SIZE,fp);
fclose(fp);
// open the file for read
if ( (fp = fopen(file, "rb")) == NULL )
{
printf("error open the file for read/n");
exit(1);
}
printf("enter a index from 0 to %d :", NUMBER_SIZE - 1);
scanf("%d", &index);
pos = index * sizeof(double);
fseek(fp, pos, SEEK_SET);
fread(&value, sizeof(double), 1, fp);
printf("the number is %f/n", value);
fclose(fp);
printf("done/n");
exit(0);
}

<2>. io 函数分析

下面是在我的ubuntu上的<stdio.h>文件比较重要函数:

/* End of file character. 定义文件结尾
Some things throughout the library rely on this being -1. */
#ifndef EOF
# define EOF (-1)

#endif

/* The possibilities for the third argument to `fseek'. 定义在fseek函数中使用的产量
These values should not be changed. */
#define SEEK_SET 0 /* Seek from beginning of file.文件开始 */
#define SEEK_CUR 1 /* Seek from current position.当前文件指针位置 */

#define SEEK_END 2 /* Seek from end of file. 文件结尾 */

/* Rename file OLD to NEW. 重命名文件 */

extern int rename(__const char *__old, __const char *__new) __THROW;
/* Close STREAM.
关闭文件流
This function is a possible cancellation point and therefore not
marked with __THROW. */

extern intfclose(FILE *__stream);

/* Flush STREAM, or all streams if STREAM is NULL.
刷新缓冲区,如果参数为null的话,将刷新所有的缓冲区
This function is a possible cancellation point and therefore not
marked with __THROW. */

extern intfflush(FILE *__stream);

/* 打开文件,__filename带打开的文件名,__modes打开方式,例如读写*/

extern FILE * fopen(__const char *__restrict __filename,

__const char *__restrict __modes) __wur;

/* If BUF is NULL, make STREAM unbuffered.更改标准 io的缓存方式
Else make it use buffer BUF, of size BUFSIZ. */

extern voidsetbuf(FILE *__restrict __stream, char *__restrict __buf) __THROW;

/* If BUF is NULL, make STREAM unbuffered.

Else make it use SIZE bytes of BUF for buffering. */
extern void setbuffer(FILE *__restrict __stream, char *__restrict __buf,
size_t __size) __THROW;
/* Make STREAM line-buffered. 设置成行缓存方式 */
extern void setlinebuf(FILE *__stream) __THROW;
/* Write formatted output to S. 一个比较常用的函数,格式化一个字符串,类似于c#中的String.Format */
extern int sprintf (char *__restrict __s,

__const char *__restrict __format, ...) __THROW;

/* Maximum chars of output to write in MAXLEN. 和函数sprintf类似,但是更加安全,增加了__maxlen参数*/
extern int snprintf(char *__restrict __s, size_t __maxlen,

__const char *__restrict __format, ...)

/* Read a character from STREAM.
从流中读取字符
These functions are possible cancellation points and therefore not
marked with __THROW. */
extern int fgetc(FILE *__stream);

extern intgetc(FILE *__stream);

/* Write a character to STREAM.
向流中写入字符
These functions are possible cancellation points and therefore not
marked with __THROW.
These functions is a possible cancellation point and therefore not
marked with __THROW. */
extern int fputc(int __c, FILE *__stream);

extern intputc(int __c, FILE *__stream);

/* Get a newline-terminated string of finite length from STREAM.
读取一行,s为开始指针,n为缓存长度
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern char * fgets(char *__restrict __s, int __n, FILE *__restrict __stream)
__wur;

/* 向流中写入字符串 */

extern intfputs(__const char *__restrict __s, FILE *__restrict __stream);

/* Push a character back onto the input buffer of STREAM.
向流中回退一个字符
This function is a possible cancellation points and therefore not
marked with __THROW. */

extern intungetc(int __c, FILE *__stream);

/***************************二进制读取写入******************************************/

/* Read chunks of generic data from STREAM.
This function is a possible cancellation points and therefore not
marked with __THROW. */
extern size_t fread(void *__restrict __ptr, size_t __size,
size_t __n, FILE *__restrict __stream) __wur;
/* Write chunks of generic data to STREAM.
ptr读取缓存区开始位置,size:单位的长度,n读取数量
This function is a possible cancellation points and therefore not
marked with __THROW. */
extern size_t fwrite(__const void *__restrict __ptr, size_t __size,

size_t __n, FILE *__restrict __s);

/********************************流指针操作**************************************/

/* Seek to a certain position on STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fseek(FILE *__stream, long int __off, int __whence);
/* Return the current position of STREAM.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern long int ftell(FILE *__stream) __wur;
/* Rewind to the beginning of STREAM.
将流指针回到文件开始
This function is a possible cancellation point and therefore not
marked with __THROW. */

extern voidrewind(FILE *__stream);

/*****************************另外和文件锁相关的函数************************************/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值