fopen打开文件失败 php,fopen()打开文件失败解决方案

编程入门 行业动态 更新时间:2024-10-06 14:31:40

fopen打开<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件失败 php,fopen()打开文件失败解决方案"/>

fopen打开文件失败 php,fopen()打开文件失败解决方案

fopen()打开文件失败

#include

#include

#define CNTL_Z '\032'

#define SLEN 50

int main(void)

{

char file[SLEN];

char ch;

FILE *fp;

long count,last;

puts("Enter the name of file to be processed");

fgets(file,SLEN,stdin);

if((fp=fopen(file,"rb"))==NULL)

{

fprintf(stderr,"打开文件\"%s\"失败\n",file);

exit(1);

}

fseek(fp,0L,SEEK_END);          //定位到文件结尾处

last=ftell(fp);

for(count=1L;count<=last;count++)

{

fseek(fp,-count,SEEK_END);

ch=getc(fp);

if(ch!=CNTL_Z&&ch!='r')

putchar(ch);

}

putchar('\n');

if(fclose(fp)!=0)

{

fprintf(stderr,"关闭文件\"%s\"失败\n",file);

exit(2);

}

return 0;

}

为什么我一直在第一个if语句结束。

运行情况:

reverse.c就是源代码文件。

我后来用vs2013编译,竟然fopen()函数编译不过,要我使用fopen_s()函数。

------解决方案--------------------

因为用fgets读取字符串,会自动在字符串后添加'\0'结束符,假设文件名为test.h,则读取后的字符串为“test.h\0”,所以打开文件会失败!

而用scanf读取就是“test.h”。

更多推荐

fopen打开文件失败 php,fopen()打开文件失败解决方案

本文发布于:2024-02-27 20:58:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1766222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   解决方案   fopen   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!