c语言学习笔记二十六

2 标准I/O库函数
 文件的基本概念
   文本文件(源文件)
   二进制文件(目标文件,可执行文件,库文件)
   查看二进制文件: hexdump 


   示例代码如下:
   
   编辑文件:vi
yuezhenhua@ubuntu:/opt/sdk/tc$ vi textfile
        1234
   查看文件长度:ls -l 文件名
        yuezhenhua@ubuntu:/opt/sdk/tc$ ls -l textfile
-rw-rw-r-- 1 yuezhenhua yuezhenhua 5 2012-12-02 17:48 textfile


   查看文件的内容:od -txl -tc -Ax 文件名
   yuezhenhua@ubuntu:/opt/sdk/tc$ od -tx1  -tc -Ax textfile
000000  31  32  33  34  0a
         1   2   3   4  \n
000005
    参数-tx1 表示文件中的字节以十六进制的形式列出来,
        -tc  表示将文件中的asscii码以字符形式列出来
        -ax  表示以十六进制的形式显示文件的地址 








  fopen/fclose
  fopen打开文件
  函数原型:
   #include <stdio.h>
   /*path文件路径
       可以是相对路径,也可以是绝对路径
查看shell进程的当前路径为 pwd
        yuezhenhua@ubuntu:/opt/sdk/tc$ od -tx1  -tc -Ax textfile
000000  31  32  33  34  0a
         1   2   3   4  \n
000005
     mode打开方式 是读还是写
      w 为写 不存在则创建
      r 为读 文件存在
      a 为追加  不存在则创建
      t 为文本文件 
      b 为二进制文件
      r+ 允许读写,文件必须存在
      w+ 允许读写,不存在就创建,如果已存在把文件长度截断为0再重新写
      a+ 允许读和追加数据 文件不存在就创建      
     FILE是标准库中的结构体类型
     调用者不应该直接方问结构体成员,在面向对象的方法底论中叫封装
    
    */
   FILE *fopen(const char *path,const char *mode);


  文件打开失败情况的处理,比如文件不存在(这里没有java中的异常来的实在)
   if(fp=fopen("/url/filename","r")==NULL){
 printf("error open file /url/filename \n");
exit(1);
   }
  
  fclose关闭文件
    函数原型:
    #include <stdio.h>
    /*成功返回0,出错返回EOF设置errorno,eof在stdio.h中定义值为-1*/
    int fclose(FILE *fp);
   


  stdin/stdout/stderr
  
  查看当前设备相关的络端设备
  yuezhenhua@ubuntu:/opt/sdk/tc$ ls -l /dev/tty
crw-rw-rw- 1 root tty 5, 0 2012-12-02 17:43 /dev/tty
  5表示主设备号
  0表示次设备号
  
  程序启动时(在main函数开始执行前)会自动把终端设备打开三次
  分别赋值给三个FILE *指针stdin stdout stderr
  所以在使用printf和scanf时不用打开终端设备
  printf向stdout写
  scanf从stdin读
  
   文件打开错误的异常也可改写如下:
    if(fp=fopen("/url/filename","r")){
        fput("error open file /url/filename\n",stderr);
exit(1);    
    }
  用户也可以直接使用这三个指针




  errno/perror函数
   系统函数在错误返回时将错误原因记录在libc定义的全局变量errno中,
   可以查阅error(3)的manpage查看各种错误码
    errno在errno.h中声明,是一个整型变量
   可以使用perror或是strerror函数将errno解释成errno
   
    #include <stdio.h>
    void perror(const char *s);
    
   示例代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>


int main(void){
FILE *fp=fopen("abcdef","r");
if(fp==NULL){
perror("open file abcdef");
printf("errno: %d\n",errno);
exit(1);
}
return 0;
}
    运行结果:
yuezhenhua@ubuntu:/opt/sdk/tc/file$ ./test_errno 
open file abcdef: No such file or directory
errno: 2


  strerror可以根据错误号返回错误字串
    示例代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>


int main(void){
FILE *fp=fopen("abc","r");
if(fp==NULL){
fputs(strerror(errno),stderr);
printf("\n");
}
return 0;
}
    运行结果如下:
yuezhenhua@ubuntu:/opt/sdk/tc/file$ gcc test_errno2.c -o test_errno2
yuezhenhua@ubuntu:/opt/sdk/tc/file$ ./test_errno2 
No such file or directory




以字节为单为的I/O函数
 从指定的文件中读一个字节
   #include <stdio.h>
   int fgetc(FILE *stream);
   int getchar(void);
 注:文件打开方式必需是可读的
     文件打开时读写位置是0,每调用一次fgetc位置会向后移动一个字节,可连续读取多个


字节
  向指定文件写一个字节
  #include <stdio.h>
  int fputc(int c,FILE *stream); 
  int putchar(int c);




 文件读写示例代码:
#include <stdio.h>
#include <stdlib.h>


int main(void){
FILE *fp;
int ch;


if((fp=fopen("testfile","w+"))==NULL){
perror("open file testfile\n");
exit(1);
}
while((ch=getchar())!=EOF){
fputc(ch,fp);
}
/*把读写位置称动到文件开头*/
  rewind(fp);
while((ch=fgetc(fp))!=EOF){
putchar(ch);
}
fclose(fp);
return 0;
}
 运行结果如下:
  yuezhenhua@ubuntu:/opt/sdk/tc/file$ ./test_fput 
hello world (ctrl+d)
hello world






 操作读写位置的函数
  #include <stdio.h>
  /*
    offset: 负数表示向前
    whence: SEEK_SET从文件开头称动offset个字节
            SEEK_CUR从当前位置移动offset个字节
            SEEK_END从文件末尾移动offset个字节 
   */
  int fseek(FILE *stream, long offset,int whence);
  int ftell(FILE *stream);
  void rewind(FILE *fp);




  示例代码如下:
#include <stdio.h>
#include <stdlib.h>


int main(void){
    FILE *fp;
    if((fp=fopen("testfile","r+"))==NULL){
perror("open file testfile");
exit(1);
    }
    if((fseek(fp,15,SEEK_SET))!=0){
perror("seek file testfile");
exit(1);
    }
    fputc('!',fp);
    fclose(fp);
    return 0;
}
  运行结果如下:
yuezhenhua@ubuntu:/opt/sdk/tc/file$ gcc test_fseek.c -o test_fseek
yuezhenhua@ubuntu:/opt/sdk/tc/file$ ./test_fseek 
yuezhenhua@ubuntu:/opt/sdk/tc/file$ od -tx1 -tc -Ax testfile
000000  68  65  6c  6c  6f  20  77  6f  72  6c  64  0a  00  00  00  21
         h   e   l   l   o       w   o   r   l   d  \n  \0  \0  \0   !
000010


以字符串为单位的I/O 函数
#include <stdio.h>
/*从指定的文件中读一行字符到调用者的缓冲区,末尾加上\0
  s是缓冲区的首地址
  size是缓冲区的长度
  
 */
char *fgets(char *s,int size,FILE *stream);
/*从标准输入到调用者的缓冲区*/
char *gets(char *s);




#include <stdio.h>
/*向指定文件写入一个字符串
  
  
 */
int fputs(const char *s,FILE *stream);
/*向标准输出写入一个字符串*/
int puts(const char *s);




以记录为单位的I/O函数
#include <stdio.h>
/*用于读写记录,一串固定长度的字节*/
/*size 指出一条记录长度
  nmemb 指定要读写多少条记录
  ptr 用于保存记录
 */
size_t fread(void *ptr,size_t size,size_t nmemb,FILE *stream);
size_t fwrite(const void *ptr,size_t size,size_t nmemb,FILE *stream);


示例代码:
#include <stdio.h>
#include <stdlib.h>


/*将结构体写到文件中*/
struct record{
char name[10];
int age;
};


int main(void){
struct record array[2]={{"yue",18},{"zhenhua",28}};
FILE *fp=fopen("recfile","w");
if(fp==NULL){
perror("open file recfile");
exit(1);
}
fwrite(array,sizeof(struct record),2,fp);
fclose(fp);
return 0;
}
运行结果:
yuezhenhua@ubuntu:/opt/sdk/tc/fread$ gcc writerec.c -o writerec
yuezhenhua@ubuntu:/opt/sdk/tc/fread$ ./writerec 
yuezhenhua@ubuntu:/opt/sdk/tc/fread$ od -tx1 -tc -Ax recfile 
000000  79  75  65  00  00  00  00  00  00  00  00  00  12  00  00  00
         y   u   e  \0  \0  \0  \0  \0  \0  \0  \0  \0 022  \0  \0  \0
000010  7a  68  65  6e  68  75  61  00  00  00  00  00  1c  00  00  00
         z   h   e   n   h   u   a  \0  \0  \0  \0  \0 034  \0  \0  \0
000020






#include <stdio.h>
#include <stdlib.h>


/*读取文件中结构体中的数据*/
struct record{
char name[10];
int age;
};


int main(void){
struct record array[2];
FILE *fp=fopen("recfile","r");
if(fp==NULL){
perror("open file recfile");
exit(1);
}
fread(array,sizeof(struct record),2,fp);
printf("name1:%s\tage1:%d\n",array[0].name,array[0].age);
printf("name2:%s\tage2:%d\n",array[1].name,array[1].age);
fclose(fp);
return 0;
}
运行结果:
yuezhenhua@ubuntu:/opt/sdk/tc/fread$ gcc readrec.c -o readrec
yuezhenhua@ubuntu:/opt/sdk/tc/fread$ ./readrec 
name1:yueage1:18
name2:zhenhuaage2:28




格式化I/O函数
#include <stido.h>
/*打印到标准输出*/
int printf(const char *format,...);
/*打印到指定的文件中*/
int fprintf(FILE *stream,const char *format,...);
/*打印用户提供的缓冲区,末尾添加\0*/
int sprintf(char *str,const char *format,...);
/*同上,可以指定缓冲区长度,字串长度不含\0*/
int snprintf(char *str,size_t size,const char *format,...);


#include <stdarg.h>


int vprintf(const char *format,va_list ap);
int vfprintf(FILE *stream,const char *format,va_list ap);
int vsprintf(char *str,const char *format,va_list ap);
int vsnprintf(char *str,size_t,size,const char *fomat,va_list ap);


#include <stdio.h>


int scanf(const char *format,...);
int fscanf(FILE *stream,const char *format,...);
int sscanf(const char *str,const char *format,...);


#include <stdarg.h>
int vscanf(const char *format,va_list ap);
int vsscanf(const char *str,const char *format,va_list ap);
int vfscanf(FLIE *stream,const char *format,va_list ap);


c标准库的I/O缓冲区
#include <stdio.h>
/*程序不会打印到屏幕 */
int main(){
printf("hello world!");
while(1);
return 0;
}
会写到终端设备的情况:
有换行符
程序退出时
调用库函数从无缓冲区的文件中读取
从行缓冲的文件中读取,并这次操作会引发系统调用从内核读取数据
手动调用flush操作


#include <stdio.h>

int fflush(FILE *stream);


3 数值字符串转换函数
#include <stdio.h>
/*把一个字符串开头可以识别成十进制整数的部分转换成int型
  如果没有可识别的整数,返回0
 */
int atoi(const char *nptr);
/*把一个字符串开头可以识别成浮点数的部分转换成double*/
double atof(const char *nptr);




#include <stdio.h>
/*是atoi的增强版,
  可以识别多进制整数
 */
long int strtol(const char *nptr,char **endptr,int base);
double strtod(const char *nptr,char **endptr);
4 分配内存的函数
除了malloc外,c标准库还提供了两个在堆空间分配内存的函数,可以用free释放
#include <stilib.h>
void *calloc(size_t nmemb,size_t size);
void *realloc(void *ptr,size_t size);


#include <alloca.h>
/*在调用者函数的栈帧上分配空间*/
void *alloca(size_t size);

posted @ 2012-12-03 21:42  retacn_yue  阅读(213)  评论(0编辑  收藏  举报