问题描述
- visual studio 2013中freadfwrite的错误
-
#include "stdafx.h"
#include
#include
struct friends
{
char name[3];
int age;
char sex[3];
int qq;
};
struct friends fri1[2], fri2[2], fr1, *fr2;
int main()
{
fr1 = fri1;
fr2 = fri2;//it is necessary
FILE fp;
char fname[30];//like can not use
printf("Please input file namen");
int t=fopen_s(&fp,"D:Visual Studio 2013ProjectFilefreadfwrite1.txt", "wt+");//like can not use fanme ....
printf("t is %dn", t);
if (t != 0)
{
printf("The file can not be opened");
getchar();
exit(1);//stdlib
}
for (int i = 0; i < 2; i++,fr1++)//fr1++
{
printf("Please input the %dst date name age sex qqn", i);
printf("hin");
scanf_s("%s %d %s %d", fr1 -> name, &fr1 -> age, fr1 -> sex, &fr1 -> qq);//not [] need&
printf("hellon");
}
for (int i = 0; i < 2; i++, fr2++)
{
printf("name age sex qq");
printf("%s%d%c%dn", fr1->name, fr1->age, fr1->sex, fr1->qq);//not [] need&
}
fwrite(fr1, sizeof(struct friends), 2, fp);
rewind(fp);//rewind the position
fread(fr2, sizeof(struct friends), 2, fp);
for (int i = 0; i < 2; i++, fr2++)
{
printf("name age sex qq");
printf("%s%d%c%dn", fr2->name, fr2->age, fr2->sex, fr2->qq);
}
fclose(fp);
return 0;
}
解决方案
不能运行,在线等回答
解决方案二:
你这个错误太多了
#include "stdafx.h"
struct friends
{
char name[10];
int age;
char sex[10];
int qq;
};
struct friends fri1[2], fri2[2], *fr1, *fr2;
int main()
{
fr1 = fri1;
fr2 = fri2;//it is necessary
FILE *fp;
char fname[30];//like can not use
printf("Please input file namen");
int t=fopen_s(&fp,"C:123.txt", "wt+");//like can not use fanme ....
printf("t is %dn", t);
if (t != 0)
{
printf("The file can not be opened");
getchar();
return 0;
}
for (int i = 0; i < 2; i++,fr1++)//fr1++
{
printf("Please input the %dst date: name age sex qqn", i+1);
printf("hin");
scanf_s("%s %d %s %d", &fr1 -> name,9, &fr1 -> age, &fr1 -> sex,9, &fr1 -> qq);//not [] need&
printf("hellon");
}
fr1 = fri1;
for (int i = 0; i < 2; i++, fr1++)
{
printf("name:%s age:%d sex:%s qq:%dn", fr1->name, fr1->age, fr1->sex, fr1->qq);//not [] need&
}
fr1 = fri1;
fwrite(fr1, sizeof(struct friends), 2, fp);
rewind(fp);//rewind the position
fread(fr2, sizeof(struct friends), 2, fp);
for (int i = 0; i < 2; i++, fr2++)
{
printf("name:%s age:%d sex:%s qq:%dn", fr2->name, fr2->age, fr2->sex, fr2->qq);
}
fclose(fp);
return 0;
}
解决方案三:
功能不是重点,重点是用vs2013 不能运行,我调试了20分钟都没弄好,么么哒