zoukankan      html  css  js  c++  java
  • 关于文件流的简单操作

    2015.1.28
    星期三 小雪
    变量可以理解为内存
    gcc -Wall 打开所有警告

    指针数组:注意指针数组是以一个NULL指针结束的; c和指针 P105

    给定一个指向以NULL结尾的指针列表的指针strings,在列表中的字符串查找一个特定的字符
    #include <stdio.h>
    #define TRUE 1
    #define FALSE 0

    int find_char(char **strings,char value)
    {
    char *string;
    while((string = *strings++) != NULL)
    {
    while(*string != '')
    {
    if(*string++ == value)
    {
    return TRUE;
    }
    }
    }
    return FALSE;
    }

    用getc和putc将标准输入复制到标准输出:

    int main()
    {
    int c;
    while((c = getc(stdin)) != EOF)
    {
    if(putc(c,stdout) == EOF)
    {
    err_sys("output error");
    }
    }
    if(ferror(stdin))
    {
    err_sys("input error");
    }
    exit(0);

    }

    int main()
    {
    char buf[MAXLINE];
    while(fgets(buf,MAXLINE,stdin) != NULL)
    {
    if(fputs(buf,stdout) == EOF)
    {
    err_sys("output error");
    }
    }
    if(ferror(stdin))
    {
    err_sys("input error);
    }
    exit(0);
    }

    下面两个函数执行二进制I/O操作:
    size_t fread(void *restrict ptr,size_t size,sizez_nobj,FILE *restrict fp)
    size_t fwrite(const void *restrict ptr,size_t size,sizez_nobj,FILE *restrict fp)
    两个函数的返回值:读或者写的对象数
    size :为每个数组元素的长度,nobj:欲写的元素数

    读或者写一个二进制数组,列如:将一个浮点型数组的第2~5个元素写至一个文件上:
    float data[10];
    if(fwrite(&data[2],sizeof(float),4,fp) != 4)
    err_sys("fwrite ,error");

    读写一个结构:
    struct {
    short count;
    long total;
    char name[NAMESIZE];
    }item;

    if(fwrite(&iteml,sizeof(item),1,fp) != 1)
    err_sys("fwrite error");

    定位流:
    long ftell(FILE *fp)
    成功:返回文件当前位置;出错:-1L
    int fseek(FILE *fp,long offset,int whence);
    成功:返回0,出错:非0

    void rewind(FILE *fp);
    将流设置为文件的起始位置

    int fgetpos(FILE *restrict fp, fpos_t *restrict pos);
    int fsetpos(FILE * fp, fpos_t *restrict pos);
    成功:0,出错:非0
    fgetpos将文件位置指示器的位置存入有pos指向的对象中,在以后调用fsetpos时,就可以使用此值将流从新定位至该位。

    int fileno(FILE *fp)
    对一个流调用fileno函数获得其描述符。

    char a[]="12345" sizeof(a) = 6

    int atexit(void (*func)(void)); 登记函数,exit调用这些函数的时候的顺序用它登记时的顺序相反

    递归函数调用的时候就会用一个新的栈帧!不影响另一个函数调用实例中的变量!

    子进程对变量的改变不影响父进程

    **********************************************************************************************************************************************************
    **********************************************************************************************************************************************************
    **********************************************************************************************************************************************************

  • 相关阅读:
    Java 判断日期的方法
    HTTP Session例子
    Unity3D性能优化
    TCP学习之二:客户端与服务端的连接
    TCP学习之一:TCP网络编程概念
    TCP学习之三:客户端、服务端同步传输字符串
    TCP学习之四:传输协议
    Lua与.net的CLR相互调用
    HTC vive开发:关于手柄按键
    JavaSESocket编程之简易聊天系统
  • 原文地址:https://www.cnblogs.com/cnlg/p/4261279.html
Copyright © 2011-2022 走看看