标准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 int fclose (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 int fflush (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 void setbuf (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 int getc (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 int putc (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 int fputs (__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 int ungetc (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 void rewind (FILE *__stream); 

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

 

posted @   qiang.xu  阅读(2355)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示