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 fileif ( (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 filefclose(fp);// print how many chars in the fileprintf("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 arrayint i;for (i = 0; i < NUMBER_SIZE; ++i){ numbers[i] = i;}// open fileif ( (fp = fopen(file, "wb")) == NULL ){ printf("error to open the file for writing\n"); exit(1);}// write the data to filefwrite(numbers, sizeof(double), NUMBER_SIZE,fp);fclose(fp);// open the file for readif ( (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_SET0/* Seek from beginning of file.檔案開始 */#define SEEK_CUR1/* Seek from current position.當前檔案指標位置 */
#define SEEK_END2/* 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);
/*****************************另外和檔案鎖相關的函數************************************/