try it, then you know it____________just do it , as love easily fade.

一次只有一个目标,步子迈小些,抑制一次实现所有功能的冲动。 过程如何结果就如何,行动是欢喜的,不管是兴奋还是沮丧的情绪都对结果无益。贵在持之以恒

导航

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\n",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("\n");
/* when parameter is a file name.
            if( (fp = fopen(*++argv,"r")) == NULL)
            {
                printf("can't open %s\n", *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\n",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文件中直接看到数值,但是呢

 

posted on 2015-05-10 23:06  吾一  阅读(144)  评论(0编辑  收藏  举报