为什么fopen打不开文件呢?

Woo_Xenia 2016-04-06 09:04:20
fp我设置的是全局变量 因为在主函数中也要使用,这里filepath.txt是在exe同一个文件夹下面的.
但是不管怎么改我的fp一直都是空.

FILE *fp;
IplImage *img = NULL ;
CvPoint pt1 = cvPoint(0,0);
CvPoint pt2 = cvPoint(0,0);
bool is_selecting = false;

//载入文件存储文件
void LoadImage()
{
fp = fopen("filepath.txt","r");
char path[50];
if(fp == NULL)
printf("wrong");
if(fp)
fscanf(fp,"%s",path);
img = cvLoadImage(path,1);
cvShowImage("img",img);
}
...全文
453 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 11 楼 paschen 的回复:
[quote=引用 6 楼 sinat_29288207 的回复:]
[quote=引用 2 楼 qq423399099 的回复:]
第一种方法:楼主这种,直接放在DEBUG目录下不行的,程序不会搜索那个目录(除非你配置过这个目录)
可以放到工程文件所在目录(.vcxproj或者.cpp等源文件的这个目录下)
第二种方法:写上filepath.txt的完整路径


我试过完整路径也不行好像[/quote]

路径中的 \ 有没有写成 \\[/quote]
我试了 也不行啊
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("c:\\rap.txt","r");
if(fp==NULL)
printf("\a文件打开失败。\n");
else{
fclose(fp);
}
return(0);
}
paschen 2016-04-08
  • 打赏
  • 举报
回复
引用 6 楼 sinat_29288207 的回复:
[quote=引用 2 楼 qq423399099 的回复:] 第一种方法:楼主这种,直接放在DEBUG目录下不行的,程序不会搜索那个目录(除非你配置过这个目录) 可以放到工程文件所在目录(.vcxproj或者.cpp等源文件的这个目录下) 第二种方法:写上filepath.txt的完整路径
我试过完整路径也不行好像[/quote] 路径中的 \ 有没有写成 \\
苏叔叔 2016-04-08
  • 打赏
  • 举报
回复
极有可能是路径、路径、路径不对!
  • 打赏
  • 举报
回复
用 wb 看你文件写在哪了
用户 昵称 2016-04-08
  • 打赏
  • 举报
回复
写一个函数,将相对路径转换为绝对路径。
小灸舞 版主 2016-04-07
  • 打赏
  • 举报
回复
引用 6 楼 sinat_29288207 的回复:
[quote=引用 2 楼 qq423399099 的回复:] 第一种方法:楼主这种,直接放在DEBUG目录下不行的,程序不会搜索那个目录(除非你配置过这个目录) 可以放到工程文件所在目录(.vcxproj或者.cpp等源文件的这个目录下) 第二种方法:写上filepath.txt的完整路径
我试过完整路径也不行好像[/quote] 贴一下你完整路径的那行代码,放在工程目录下试过吗?
Woo_Xenia 2016-04-07
  • 打赏
  • 举报
回复
引用 2 楼 qq423399099 的回复:
第一种方法:楼主这种,直接放在DEBUG目录下不行的,程序不会搜索那个目录(除非你配置过这个目录) 可以放到工程文件所在目录(.vcxproj或者.cpp等源文件的这个目录下) 第二种方法:写上filepath.txt的完整路径
我试过完整路径也不行好像
paschen 2016-04-07
  • 打赏
  • 举报
回复
如果你是在调试程序,程序的当前路径并不是EXE所在文件夹,而是工程那个文件夹

另外打开失败可以检查fopen返回值以确定具体原因:http://en.cppreference.com/w/c/io/fopen
赵4老师 2016-04-07
  • 打赏
  • 举报
回复
_getcwd, _wgetcwd Get the current working directory. char *_getcwd( char *buffer, int maxlen ); wchar_t *_wgetcwd( wchar_t *buffer, int maxlen ); Routine Required Header Compatibility _getcwd <direct.h> Win 95, Win NT _wgetcwd <direct.h> or <wchar.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 pointer to buffer. A NULL return value indicates an error, and errno is set either to ENOMEM, indicating that there is insufficient memory to allocate maxlen bytes (when a NULL argument is given as buffer), or to ERANGE, indicating that the path is longer than maxlen characters. Parameters buffer Storage location for path maxlen Maximum length of path in characters: char for _getcwd and wchar_t for _wgetcwd Remarks The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) will automatically be allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer). _getcwd returns a string that represents the path of the current working directory. If the current working directory is the root, the string ends with a backslash ( \ ). If the current working directory is a directory other than the root, the string ends with the directory name and not with a backslash. _wgetcwd is a wide-character version of _getcwd; the buffer argument and return value of _wgetcwd are wide-character strings. _wgetcwd and _getcwd behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tgetcwd _getcwd _getcwd _wgetcwd Example // GETCWD.C /* This program places the name of the current directory in the * buffer array, then displays the name of the current directory * on the screen. Specifying a length of _MAX_PATH leaves room * for the longest legal path name. */ #include <direct.h> #include <stdlib.h> #include <stdio.h> void main( void ) { char buffer[_MAX_PATH]; /* Get the current working directory: */ if( _getcwd( buffer, _MAX_PATH ) == NULL ) perror( "_getcwd error" ); else printf( "%s\n", buffer ); } Output C:\code Directory Control Routines See Also _chdir, _mkdir, _rmdir _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
小灸舞 版主 2016-04-06
  • 打赏
  • 举报
回复
VS调试时,程序的当前路径是工程路径!并不是exe所在的目录
小灸舞 版主 2016-04-06
  • 打赏
  • 举报
回复
第一种方法:楼主这种,直接放在DEBUG目录下不行的,程序不会搜索那个目录(除非你配置过这个目录)
可以放到工程文件所在目录(.vcxproj或者.cpp等源文件的这个目录下)
第二种方法:写上filepath.txt的完整路径
Woo_Xenia 2016-04-06
  • 打赏
  • 举报
回复
这个只是我打开文件的小函数,fclose在main函数里面

69,371

社区成员

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

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