String Compare and File Handling mixing together...
I am practicing my self to combine functions together like this.
so get this, is it possible to search record from the File using File Handling(fscanf)
and compare it from the User's input and display it...check out my program.
content of 2003.txt
1234 [tab] John [tab] Doend
3214 [tab] Jane [tab] Dredy
2104 [tab] Joey [tab] Drone
here is my program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int opt,opt1,a;
int x;
char fn[15],ln[15],sn[15],data[15],line[225];
char locate[15];
FILE *f03,*f04,*f05;
FILE *s13,*s23,*s33;
FILE *s14,*s24,*s34;
FILE *s15,*s25,*s35;
printf("\t\t\t\t*****MENU*****");
printf("\n\n\n");
printf("\t\t\t\t***STUDENT PROFILE***\n\n");
f03=fopen("2003.txt","r");
printf("2003 Student Enrollies\n\n");
printf("Student Number \t\tFirst Name \t\tLast Name\n\n");
while(!feof(f03))
{
fscanf(f03,"%s\t%s\t%s\n",&sn,&fn,&ln);
printf("\n%s\t\t\t%s\t\t\t%s",sn,fn,ln);
}
fclose(f03);
printf("\n\nEnter Data to Display: ");
scanf("%s",&data);
system("cls");
if((strcmpi(data,"1234")==0)||(strcmpi(data,"John")==0)||(strcmpi(data,"Doend")==0))
{
s13=fopen("03/1234.txt","r");
while(fgets(line, sizeof(line), s13) != NULL)
{
printf("%s",line);
}
fclose(s13);
}
else if((strcmpi(data,"3214")==0)||(strcmpi(data,"Jane")==0)||(strcmpi(data,"Dredy")==0))
{
s23=fopen("03/3214.txt","r");
while(fgets(line, sizeof(line), s23) != NULL)
{
printf("%s",line);
}
fclose(s23);
}
else if((strcmpi(data,"2104")==0)||(strcmpi(data,"Joey")==0)||(strcmpi(data,"Drone")==0))
{
s33=fopen("03/2104.txt","r");
while(fgets(line, sizeof(line), s33) != NULL)
{
printf("%s",line);
}
fclose(s33);
}
else
{
printf("\n\n\t\t\tNo record Exist\n\n\n");
}
printf("\n\n");
system("pause");
}
if you can see here:
if((strcmpi(data,"1234")==0)||(strcmpi(data,"John")==0)||(strcmpi(data,"Doend")==0))
I already inputed the '1234','John' and 'Doend'...hence the program is long and I may want add a EDITING function to my program.
My question is, can I change them into a single variable EACH?
example '1234' is variable 'sn', 'John' is variable 'fn' and 'Doend' is variable 'ln'
and I can still compare it and open the correct file for it.
the correct file to open for John is 03/1234.txt
if that can happen my program will be shorter right? less hassle for someone.
Thank you in advance to everyone willing to help me with this trouble and also to Daniweb.com \m/