本文介绍了函数主体中不允许使用文件指针和结构变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里我正在c学校管理申请。
bt我已经达到了一个真正贬低我的点
它给了我一个错误,如声明不允许这里同时使用文件指针和结构变量声明...
i不能理解这个错误让我烦恼..
here i m making an application on school mgmt in c .
bt i have reached a point that really herrasing me
it gives me a error like declaration not allowed here while using file pointer and structure variable declaration ...
i cant understand this error is annoying me..
void addrecord( )
{
system("cls");
FILE *fp ;// here error ocured
char another = 'Y' ,time[10];
struct record e ;// error
char filename[15];
int choice;
printf("\n\n\t\t***************************\n");
printf("\t\t* WELCOME TO THE ADD MENU *");
printf("\n\t\t***************************\n\n");
printf("\n\n\tENTER DATE OF YOUR RECORD:[yyyy-mm-dd]:");
fflush(stdin);
gets(filename);
fp = fopen (filename, "ab+" ) ;
if ( fp == NULL )
{
fp=fopen(filename,"wb+");
if(fp==NULL)
{
printf("\nSYSTEM ERROR...");
printf("\nPRESS ANY KEY TO EXIT");
getch();
return ;
}
}
while ( another == 'Y'|| another=='y' )
{
choice=0;
fflush(stdin);
printf ( "\n\tENTER TIME:[hh:mm]:");
scanf("%s",time);
rewind(fp);
while(fread(&e,sizeof(e),1,fp)==1)
{
if(strcmp(e.time,time)==0)
{
printf("\n\tTHE RECORD ALREADY EXISTS.\n");
choice=1;
}
}
if(choice==0)
{
strcpy(e.time,time);
printf("\tENTER NAME:");
fflush(stdin);
gets(e.name);
fflush(stdin);
printf("\tENTER PLACE:");
gets(e.place);
fflush(stdin);
printf("\tENTER DURATION:");
gets(e.duration);
fflush(stdin);
printf("\tNOTE:");
gets(e.note);
fwrite ( &e, sizeof ( e ), 1, fp ) ;
printf("\nYOUR RECORD IS ADDED...\n");
}
printf ( "\n\tADD ANOTHER RECORD...(Y/N) " ) ;
fflush ( stdin ) ;
another = getchar( ) ;
}
fclose ( fp ) ;
printf("\n\n\tPRESS ANY KEY TO EXIT...");
getch();
}
推荐答案
system("cls"); // <<======= MOVE THIS LINE
FILE *fp ;// here error ocured
char another = 'Y' ,time[10];
struct record e ;// error
char filename[15];
int choice;
// <<======= HERE
正如 nv3 所述, C
编程语言要求变量声明在块的最开头(实际上, C99
兼容编译器不应该要求)。
As already noted by nv3, the C
programming language requires variable declarations be at the very beginning of the block (well, actually C99
compliant compilers should not require that).
这篇关于函数主体中不允许使用文件指针和结构变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!