C语言用fopen以只读方式打开文件,程序闪退问题

E_Herohawk 2014-12-07 10:21:28
写一个C语言控制台程序,在脱离VC6.0运行环境打开.exe文件时程序闪退,首先确定的是加了conio.h这个头文件了,在return 0;前写了getch();了。出现闪退我发现是用fopen以只读方式打开一个文件时就会发生这个现象,如果是只写呢,就没事,只要程序里有读文件的操作就会闪退,为了表达清楚我还是贴代码吧,尽管代码可以说基本没有东西。
int main(void)
{
FILE *fpr, *fpw;

if((fpr = fopen("file1.txt","r")) == NULL)
{
printf("It can not be opened the file!\n");
exit(0);
}
/* if((fpw = fopen("file2.txt","w")) == NULL)
{
printf("It can not be opened the file!");
exit(0);
}*/

//fclose(fpw);
fclose(fpr);

getch();
//while(1);
return 0;
}
代码就这个,啥也没干,原本我是操作文件了的,但是我把程序摘成这样,还是会闪退,即使返回前加死循环也是闪退。不知道为什么,希望大侠们能指点指点小弟,小弟不胜感激。
...全文
726 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
头文件 #include "conio.h" 末尾:getch();
E_Herohawk 2014-12-08
  • 打赏
  • 举报
回复
嗯,其实我被一个问题给蒙蔽了,在VC环境下运行程序时打开文件是在工作空间或工程文件夹下查找文件,所以能正常执行,而脱离VC环境运行程序时打开文件是在可执行文件当前文件夹下查找文件,而我要读的文件是放在工程文件夹下不是放在debug这个文件夹下所以打开出错,程序就直接退出了。
图灵狗 2014-12-08
  • 打赏
  • 举报
回复
文件路径问题。
li4c 2014-12-08
  • 打赏
  • 举报
回复
你要先e生成release版本才可以脱离vc6打开,我估计你的是debug版本的http://www.cxybl.com/html/bcyy/c/20130605/38340.html
赵4老师 2014-12-08
  • 打赏
  • 举报
回复
_chdir, _wchdir Change the current working directory. int _chdir( const char *dirname ); int _wchdir( const wchar_t *dirname ); Routine Required Header Optional Headers Compatibility _chdir <direct.h> <errno.h> Win 95, Win NT _wchdir <direct.h> or <wchar.h> <errno.h> Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a value of 0 if successful. A return value of –1 indicates that the specified path could not be found, in which case errno is set to ENOENT. Parameter dirname Path of new working directory Remarks The _chdir function changes the current working directory to the directory specified by dirname. The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive and if a new drive letter is specified in dirname, the default drive letter will be changed as well. For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive: _chdir("c:\\temp"); When you use the optional backslash character (\) in paths, you must place two backslashes (\\) in a C string literal to represent a single backslash (\). _wchdir is a wide-character version of _chdir; the dirname argument to _wchdir is a wide-character string. _wchdir and _chdir behave identically otherwise. Generic-Text Routine Mapping: TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tchdir _chdir _chdir _wchdir Example /* CHGDIR.C: This program uses the _chdir function to verify * that a given directory exists. */ #include <direct.h> #include <stdio.h> #include <stdlib.h> void main( int argc, char *argv[] ) { if( _chdir( argv[1] ) ) printf( "Unable to locate the directory: %s\n", argv[1] ); else system( "dir *.wri"); } Output Volume in drive C is CDRIVE Volume Serial Number is 0E17-1702 Directory of C:\write 04/21/95 01:06p 3,200 ERRATA.WRI 04/21/95 01:06p 2,816 README.WRI 2 File(s) 6,016 bytes 71,432,116 bytes free Directory Control Routines See Also _mkdir, _rmdir, system
孤雲独去闲 2014-12-07
  • 打赏
  • 举报
回复
打开的文件不存在吧, printf("It can not be opened the file!\n"); exit(0); 在这句话中间加个getchar();就该看出来了
chatelr 2014-12-07
  • 打赏
  • 举报
回复
文件存在么,要是不存在,估计会闪退吧。可以加上r+

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧