你这个程序,要求test1.txt和你的可执行文件放到一个目录下才能打开文件,因为你打开文件时没有指定目录。一般fopen打开文件后都需要检查一下文件指针是否为空的,否则因为文件不存在等原因导致fopen失败可能导致程序走到下面实用文件指针时直接core dump。
另外,你fread直接读取一个结构体,这样的话,要求你的文件是通过fwrite直接写结构体,否则可能导致读出来的数据格式乱了,并且这个文件应该是个二进制文件而不是文本文件吧?
//#includeC语言可以不用这个
#include
struct st_cusacc
{
char accId[11];
char name[21];
char Passwd[7];
};
int main()
{
struct st_cusacc openCA[2];
FILE *fp=fopen("test1.txt","rb");
if ( fp==NULL ) //加上这个,确保文件不在时,会报错!
{
printf("file not found!\n");
return -1;
}
fread(openCA,sizeof(struct st_cusacc),2,fp);
fclose(fp);
printf("%s|%s|%s\r\n",openCA[1].accId,openCA[1].name,openCA[1].Passwd);//参数多了fp
system("pause");
return 0;
}
这是c么,iostream是c++的.#include