文件打开失败是怎么回事

bingo_life 2014-03-24 06:22:07
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
char number[20];
char name[20];
char ID[20];
int score;
struct student *next;
}student;
void sort(student *s[],int sn);
int main()
{
FILE* fd;
int flag = 0;
int sn = 40;
student *s[40];
//打开文件
if((fd = fopen("C:\\1","r"))==NULL)
{
printf("文件读取失败.\n");
return 0;
}
printf("文件读取成功.\n");

//读取文件
for( flag = 0; flag < sn; flag++)
{
fscanf(fd,"%s%s%s%s%s%d",s[flag]->number,s[flag]->name,s[flag]->ID,
&s[flag]->score);

printf("%s%16s%8s%16s%16s%8d\n",s[flag]->number,s[flag]->name,s[flag]->ID,
s[flag]->score);
}

//关闭文件
fclose(fd);


sort(s,sn);//按照分数高低输出所有学生信息


return 0;

}
void sort(student *s[],int sn)
{
student * temp;
int i=0,j=0;
for( ;i<sn-1; i++)
{
for(j = i; j<sn; j++)
{
if(s[i]->score < s[j]->score)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for(i=0;i<sn;i++)
{
printf(s[i]->number,s[i]->name,s[i]->ID,
s[i]->score);
}


}
运行显示文件读取失败,求问哪里出错了。。
...全文
726 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
水平不流 2014-03-27
  • 打赏
  • 举报
回复
perror可以看到是文件不存在,还是权限被拒绝等等之类的错误消息。
苍蝇①号 2014-03-27
  • 打赏
  • 举报
回复
perror
孤影品茗 2014-03-27
  • 打赏
  • 举报
回复
引用 10 楼 Niclezhang 的回复:
[quote=引用 4 楼 zhaowech 的回复:] 尝试用GetLastError,我知道你文件在,问题是你代码fd = fopen("C:\\1","r"))==NULL要改为 fd = fopen("C:\\1.txt","r"))==NULL。你肯定没加后缀名.txt
应该是你文件名写的不对,没有后缀名。[/quote] 是文本文件还是纯粹的文件,你的系统没有显示后缀名,不代表没有,如果getlasterror,估计是文件路径不存在。
Niclezhang 2014-03-26
  • 打赏
  • 举报
回复
引用 4 楼 zhaowech 的回复:
尝试用GetLastError,我知道你文件在,问题是你代码fd = fopen("C:\\1","r"))==NULL要改为 fd = fopen("C:\\1.txt","r"))==NULL。你肯定没加后缀名.txt
应该是你文件名写的不对,没有后缀名。
ztenv 版主 2014-03-26
  • 打赏
  • 举报
回复
看一下错误号再说
zybjtu 2014-03-26
  • 打赏
  • 举报
回复
目测是文件名不存在。只要句柄还有富裕,一般打开文件不会出错的。
buyong 2014-03-26
  • 打赏
  • 举报
回复
用资源管理器看文件名时,打开显示扩展名的选项
赵4老师 2014-03-26
  • 打赏
  • 举报
回复
perror, _wperror Print an error message. void perror( const char *string ); void _wperror( const wchar_t *string ); Routine Required Header Compatibility perror <stdio.h> or <stdlib.h> ANSI, Win 95, Win NT _wperror <stdio.h> or <wchar.h> Win 95, 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 None Parameter string String message to print Remarks The perror function prints an error message to stderr. _wperror is a wide-character version of _perror; the string argument to _wperror is a wide-character string. _wperror and _perror behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tperror perror perror _wperror string is printed first, followed by a colon, then by the system error message for the last library call that produced the error, and finally by a newline character. If string is a null pointer or a pointer to a null string, perror prints only the system error message. The error number is stored in the variable errno (defined in ERRNO.H). The system error messages are accessed through the variable _sys_errlist, which is an array of messages ordered by error number. perror prints the appropriate error message using the errno value as an index to _sys_errlist. The value of the variable _sys_nerr is defined as the maximum number of elements in the _sys_errlist array. For accurate results, call perror immediately after a library routine returns with an error. Otherwise, subsequent calls can overwrite the errno value. In Windows NT and Windows 95, some errno values listed in ERRNO.H are unused. These values are reserved for use by the UNIX operating system. See _doserrno, errno, _sys_errlist, and _sys_nerr for a listing of errno values used by Windows NT and Windows 95. perror prints an empty string for any errno value not used by these platforms. Example /* PERROR.C: This program attempts to open a file named * NOSUCHF.ILE. Because this file probably doesn't exist, * an error message is displayed. The same message is * created using perror, strerror, and _strerror. */ #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <io.h> #include <stdlib.h> #include <stdio.h> #include <string.h> void main( void ) { int fh; if( (fh = _open( "NOSUCHF.ILE", _O_RDONLY )) == -1 ) { /* Three ways to create error message: */ perror( "perror says open failed" ); printf( "strerror says open failed: %s\n", strerror( errno ) ); printf( _strerror( "_strerror says open failed" ) ); } else { printf( "open succeeded on input file\n" ); _close( fh ); } } Output perror says open failed: No such file or directory strerror says open failed: No such file or directory _strerror says open failed: No such file or directory Process and Environment Control Routines See Also clearerr, ferror, strerror
sduxiaoxiang 2014-03-25
  • 打赏
  • 举报
回复
open失败后 printf("errno is %d\n", errno); 根据errno的值 看看系统错误号
孤影品茗 2014-03-25
  • 打赏
  • 举报
回复
尝试用GetLastError,我知道你文件在,问题是你代码fd = fopen("C:\\1","r"))==NULL要改为 fd = fopen("C:\\1.txt","r"))==NULL。你肯定没加后缀名.txt
bingo_life 2014-03-25
  • 打赏
  • 举报
回复
文件确定就C盘根目录下额。。
ws_yqr 2014-03-24
  • 打赏
  • 举报
回复
楼主你C盘根目录下1这个文件吗?应该是找不到文件造成的,还有你读文件fscanf跟printf实参数据少了吧。
影子魔术师 2014-03-24
  • 打赏
  • 举报
回复
用perror或者strerror看下错误信息. perror 头文件 <errno.h> strerror 头文件<string.h> http://blog.csdn.net/kevinzhangyang/article/details/6907556

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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