C语言为什么我的文件总是读不到结尾???
总是不能正常的读到结尾,上一次写的读到结尾后再读了一次才到结尾,这一次干脆就到不了结尾了,一直无限循环。。。。fopen_s(&fp,"User.txt","rb+");...
总是不能正常的读到结尾,上一次写的读到结尾后再读了一次才到结尾,这一次干脆就到不了结尾了,一直无限循环。。。。
fopen_s (&fp, "User.txt", "rb+");
while (!feof (fp))
{
if (n == 0)
{
p = (User *) malloc (sizeof (User));
dontwant = p;
UserHead = p;
p->NextUser = NULL;
}
else
{
p->NextUser = (User *) malloc (sizeof (User));
p = p->NextUser;
p->NextUser = NULL;
}
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserLoginName, 20);
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserKey, 20);
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserName, 20);
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserPosition, 10);
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserState, 5);
fseek (fp, 2, SEEK_CUR);
n++;
printf ("1");
}
fclose (fp); 展开
fopen_s (&fp, "User.txt", "rb+");
while (!feof (fp))
{
if (n == 0)
{
p = (User *) malloc (sizeof (User));
dontwant = p;
UserHead = p;
p->NextUser = NULL;
}
else
{
p->NextUser = (User *) malloc (sizeof (User));
p = p->NextUser;
p->NextUser = NULL;
}
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserLoginName, 20);
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserKey, 20);
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserName, 20);
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserPosition, 10);
fseek (fp, 7, SEEK_CUR);
fscanf_s (fp, "%s", p->UserState, 5);
fseek (fp, 2, SEEK_CUR);
n++;
printf ("1");
}
fclose (fp); 展开
展开全部
// Try:
fopen_s (&fp, "User.txt", "rb+");
int i=0; // 新增的变量
while (!feof (fp))
{
if (n == 0)
{
p = (User *) malloc (sizeof (User));
dontwant = p;
UserHead = p;
p->NextUser = NULL;
}
else
{
p->NextUser = (User *) malloc (sizeof (User));
p = p->NextUser;
p->NextUser = NULL;
}
fseek(fp,i*sizeof(User),SEEK_SET);
fscanf_s (fp, "%s", p->UserLoginName, 20);
fseek(fp,i*sizeof(User),SEEK_SET);
fscanf_s (fp, "%s", p->UserKey, 20);
fseek(fp,i*sizeof(User),SEEK_SET);
fscanf_s (fp, "%s", p->UserName, 20);
fseek(fp,i*sizeof(User),SEEK_SET);
fscanf_s (fp, "%s", p->UserPosition, 10);
fseek(fp,i*sizeof(User),SEEK_SET);
fscanf_s (fp, "%s", p->UserState, 5);
fseek(fp,i*sizeof(User),SEEK_SET);
n++;
++i;
printf ("1");
}
fclose (fp);
更多追问追答
追问
还是不行,读不到结尾,无限循环..........
用户名:2012012
密码:123456
姓名:黄晓东
职位:经理
状态:在职
用户名:2012026
密码:123456
姓名:周晓东
职位:经理
状态:离职
用户名:2012035
密码:123456
姓名:林晓东
职位:服务员
状态:离职
追答
你是用结构体的吗?下面的语句只适合结构体
fseek(fp,i*sizeof(struct 结构体名),SEEK_SET);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |