I want to right in .dat file through structure.For this i have defined following:
struct student //structure defination
{
char name[25];
char course[25];
char rollNum[20];
};
(A) in rollNum field i am taking input in two formates.
1. 1234
2. A1234/a1234
(B)in second formate i always wants to write data in upper case for example
If user is typing b5678 ,i want to store as B5678.
(C)for input i am using these statements:
printf("\n\tEnter Roll number (Ex.1234 or a123)\n");
scanf("%s",s.rollNum);
(D)for writing on file i am using these statements:
fprintf(fp,"%s %s %s \n" ,s.rollNum,s.name,s.course);
(E)Can you please tell how is it possible to change case before writing in .dat file
I tried through toupper() and strupr() but having typecasting error.
Thanks in advance
Ankita
Post your code.
Code is
#include
#include
#include
#include
int isValid(char[],char[]);//function declaration
struct student //structure defination
{
char name[25];
char course[25];
char rollNum[20];
};
//isvalid function for checking valid rollnumber pattren and name
int isValid(char str[],char str1[]){
int i=0,j=0,flag=0,flag1=0;
for(i=0;i= 'A' && str[i] <= 'Z') ||(str[i] >= 'a' && str[i] <= 'z'))
flag=0;
else
if((str[i]>='0'&&str[i]<='9')||((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')))
flag=1;
} /*checking for valid name only characters*/
for(j=0;j='A'&&str1[j]<='Z')||(str1[j]>='a'&&str1[j]<='z'))
flag1=1;
else
flag1=0;
}
return (flag && flag1);
}
//main function
int main()
{
int choice,rCheck=0,k=0,flag;
FILE *fp;
struct student s;
char loop,search[20],rollNum1[25];
//open file in append mode
fp=fopen("Student.dat","a+");
if(fp==NULL)
{
printf("FILE CAN'T OPEN.\n");
exit(1);
}
printf("\n\tOUT PUT 2\n");
//menu
do
{flag=1;
printf("\n\t1.Add Record\n");
printf("\t2.Display Records\n");
printf("\t3.EXIT\n");
printf("\tEnter your choice...\n");
scanf("%d",&choice);
switch(choice)
{
//Add Record in file
int k;
case 1:printf("\n\tEnter Roll number (Ex.1234 or a123)\n");
//Here i want to change case
//...........................................
scanf("%s",s.rollNum);
//.....................................
fseek(fp,0,SEEK_SET);
/*searching rollnumber in file for avoiding duplicate rollnum*/
while(fscanf(fp,"%s",search)!=EOF){
if(strcmp(search,s.rollNum)==0){
printf("This roll number already exist!!!!!");
flag=0;break;}}
/* rollnumber not exist then add data*/
if(flag!=0){
printf("\n\tEnter name and subject\n");
scanf("\n\t%s %s",s.name,s.course);
rCheck=isValid(s.rollNum,s.name);
if(rCheck==1)
fprintf(fp,"%s %s %s \n" ,s.rollNum,s.name,s.course);
else
printf("\n\n!!!!Enter correct rollnumber and Name!!!!!");
}
break;
//Display records from begining
case 2:fseek(fp,0,SEEK_SET);
printf("\n\tROLLNUMBER\tNAME\t\tCOURSE\n");
while(fscanf(fp,"%s %s %s",s.rollNum,s.name,s.course)!=EOF)
printf("\n\n\t%s \t\t%5s \t\t%s \n",s.rollNum,s.name,s.course);
break;
//exit
case 3:exit(1);
/*default*/
default:printf("\n\n\t\a !!!!!! WARNING !!!!! \a\n\t");
printf("\n\t\a!!!!!! Please enter valid choice !!!!! \n\t"); /*default case*/
system("pause");
exit(1);
}
printf("\n\n\t Do you want to continue (Y/N) : ");
loop=getche();
}while(loop=='y'||loop=='Y');
//close file
fclose(fp);
}
Can anybody help me in solving this question?