stayathust

当前目录仅有四个文件:

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文件。

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

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-03-05
  • 2021-07-21
  • 2022-02-05
  • 2022-12-23
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-11-30
  • 2021-05-23
相关资源
相似解决方案