zoukankan      html  css  js  c++  java
  • file access , argc, argv[ ]

    _____main函数含有 两个参数 ,argc ,argv[]

    这两个参数用以指示命令行输入的参数信息。

    argc 的值是输入的参数的数量。argv是一个数组,每个数组元素指向一个string字符串类型的数据的地址,也就是存放每一个输入参数的地址。argv就是 char ** 类型。

    void fileCopy(FILE *ifp,FILE *ofp)
    {
        int c;
        while( (c = getc(ifp) ) != EOF)
        {
            putc(c,ofp);
        }
    
    }
    int main(int argc, char *argv[])
    {
    
        //practice file access
            //practice argc, argv
        FILE *fp;
        if(argc == 1) //no args;copy standard input
        {
            fileCopy(stdin, stdout);
        }
        else
        {
            printf("%d
    ",argc);
            while(--argc > 0)
            {
                int i;
                for(i = 0; i < argc; i++) //parameter is a string.
                {
                    printf("%s%s",argv[i],(i < argc - 1) ? " " :"");
    
                }
                printf("
    ");
    /* when parameter is a file name.
                if( (fp = fopen(*++argv,"r")) == NULL)
                {
                    printf("can't open %s
    ", *argv);
                    return 1;
                }
                else
                {
                    fileCopy(fp,stdout);
                    fclose(fp);
                }
    */
            }
        }
        return 0;
    }

    ___file access

    为了对一个文件进行存取操作,首先吸引获得这个文件的指针,这个文件指针指向一个结构类型数据,它包含了所关联文件的相关信息,包括buffer的位置,文件中当前的编辑位置,等等。用户不需要知道那些信息的细节。只需要利用库函数中结构类型FILE,只需做如下操作

    FILE *fp;
    //the open operation may not successfully, check is necessary
    if( (fp = fopen(FILE_NAME,MODE) == NULL  )
    {
        fprintf(stderr, "can'topen%s
    ",FILE_NAME); 
        exit(EXIT_FAILURE);
    }

    //after file operation, close the file
    fclose(fp);

    _ fprintf, fscanf 对应于标准输入输出的printf和scanf,参数多了最前面的一个FILE类型的参数。

    _ fputs, fgets,   simillar to the getline function

    _ fread, fwirte  has same parameters. They allow big block access to file in one step.

    make sure the file position is your wanted. use eg:  

    rewind(fp);//set file postion at the start. !!!! reset the file postion is important

    There are other liarbry functions to set file positon.

    ___存取的文件类型可以是二进制类型,或者是text类型的,eg,file.bat , or file.txt.

    text类型的文件操作时容易出问题,比如回车键之类可能会出题,对text操作有危险。

    用fprintf 能在text文件中直接看到数值,但是呢

  • 相关阅读:
    1539. Kth Missing Positive Number (E)
    0082. Remove Duplicates from Sorted List II (M)
    0526. Beautiful Arrangement (M)
    解决mac电脑耳机/外放突然无声音
    利用蒙特卡洛方法实现21点问题的最优解(内含python源码)
    浅析机器视觉在医疗影像处理中的应用
    基于最近邻法手写数字识别(内附python源码)
    最新版 | 2020李沐《动手学深度学习》中文版pdf重磅开源!
    干货收藏!639页《深度学习:Deep Learning》图文并茂课程PPT
    22课时、19大主题,CS 231n进阶版课程视频上线!
  • 原文地址:https://www.cnblogs.com/aprilapril/p/4493353.html
Copyright © 2011-2022 走看看