fopen()函数对文件名的要求。

当前目录仅有四个文件:

test.exe

temp

temp.a

temp.txt

temp.z

此时,使用fopen("temp.", "r")打开的文件为temp。

 

代码:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{

    FILE *fp;

    if((fp = fopen("tmp.","r")) == NULL)
    {
        fprintf(stderr, "Can't open file \n" );
        exit(1);
    }

    char ch;
    while((ch = getc(fp)) != EOF)
        putc(ch,stdout);

    fclose(fp);

    return 0;
}

 这就存在一个问题,当从获取文件名时,存储文件名的字符数组的长度不足以容纳文件名时,会将超出的部分舍弃(采用fgets()从输入获取或者strncpy从命令行参数获取),

原本想要打开文件temp.txt,最后打开的却是temp文件。

尚未找到好的处理方法,目前使用的是定义一个较大的文件名数组。

posted @ 2014-08-15 09:26  liuxia_hust  阅读(1783)  评论(0编辑  收藏  举报