C语言,fscanf问题

2024-11-27 10:36:42
推荐回答(3个)
回答1:

你的程序问题太多,我就不一一解释了。原来悬赏分是0的啊!哎,罢了...
反正下面程序能实现你需要的。我大略的修改,你先用着吧!
#include
#include
#include
#include
#include
struct cx
{
int month;
float jck;
float ck;
float jk;
}chaxun[8];
int i;

void TianJiaShuJu();
void search();
void add();
void del();

int main()
{
char keyboard;
printf( "欢迎使用本软件\n" );
printf( "添加数据:1,查询数据:2,删除数据:3,合计数据:4\n" );
printf( "请输入操作:\n" );
keyboard = getchar();
if( '1' == keyboard )
{
TianJiaShuJu();
}
if( '2' == keyboard )
{
search();
}
return 0;
}
void TianJiaShuJu()
{
char str[200] = "e:\\";
char txt[] = ".txt";
char year[5];
FILE *filename;

printf("请输入年份:\n");//输入四个数字字符
scanf( "%s", year );
strncat( str, year, 4 );
strcat(str, txt);

filename = fopen( str, "a+" );
if( filename == NULL )
{
printf( "Cannot open file strike any key exit!" );
getch();
exit(1);
}
for( i = 0; i <= 8; i++ )
{
printf("请输入月份:\n");
scanf( "%d", &chaxun[i].month );

printf( "请输入进出口额:\n" );
scanf( "%f", &chaxun[i].jck );

printf("请输入出口额:\n");
scanf( "%f", &chaxun[i].ck );

printf( "请输入进口额:\n" );
scanf( "%f", &chaxun[i].jk );
fwrite( &chaxun[i], sizeof(chaxun[0]), 1, filename);
}
fclose( filename );
getch();
}
void search()
{
int yuefen;
char str[200]="e:\\";
char year[5];
char txt[]=".txt";
FILE *filename;

printf( "请输入年份:\n" );//输入四个数字字符
scanf( "%s", year );
strncat( str, year, 4);
strcat(str, txt);

filename = fopen( str, "r" );
if( filename == NULL )
{
printf( "Cannot open file strike any key exit!" );
getch();
exit(1);
}
for( i = 0; i <= 8; i++ )
{
fread( &chaxun[i], sizeof(chaxun[0]), 1, filename);
printf( "%d\t%f\t%f\t%f\n", chaxun[i].month, chaxun[i].jck, chaxun[i].ck, chaxun[i].jk );
}
printf( "请输入月份:\n" );
scanf("%d", &yuefen);
if( 0 == yuefen )
{
for( i = 0; i <= 8; i++)
{
printf( "当年全年第%d月进出口额是:%f\n", chaxun[i].month,chaxun[i].jck );
printf( "当年全年第%d月出口额是:%f\n", chaxun[i].month,chaxun[i].ck );
printf( "当年全年第%d月进口额是:%f\n", chaxun[i].month,chaxun[i].jk );
}
return;
}
for( i = 0; i <= 8; i++ )
{
if( chaxun[i].month == yuefen )
{
printf("当年当月进出口额是:%f", chaxun[i].jck);
printf("当年当月出口额是:%f", chaxun[i].ck);
printf("当年当月进口额是:%f\n", chaxun[i].jk);
break;
}
}
}

回答2:

printf("请输入年份:\n");//输入四个数字字符
scanf("%*c%c%c%c%c",&year[0],&year[1],&year[2],&year[3]);
我咋看到你这%c中间夹了个*号呢?

回答3:

你应该在C\C++语言里问....