content year.txt is:
2001
2002
2003
2004
content of 2001.txt is:
0123 Codie Banks
0125 Stuid Buri
0126 Chloe huis
0127 halu Wafu
content 0123.txt:
0123 you have successfully
read what is inside the file.
Note: the file 0123.txt is inside a folder name 2001 which means it looks something like this 2001/0123.txt is my code correct:
sprintf(flnme,"%s/%s.txt",yr,sn); fp2=fopen(flnme,"r");
when you run it goes something like this:
Enter Year: 2001
Displays the year.txt content
Enter Student No.: 0123
displays the content of 0123.txt
note: it displays the content of the 0123.txt IF its not inside a folder, but i want to put it inside a folder to categorize them.
so far its running good.
but when i run it again and enter 2002,2003 or 2004. xxxxxx.exe has stopped working appears.
can you guys take a look and point out where did i go wrong in my code?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp1,*fp2,*sy;
char sn[15],fn[5],ln[5];
char yr[10],input_yr[10];
char srch[5],line[225];
char flnme[1024];
int y=0, index;
sy=fopen("year.txt","r");
while(fscanf(sy,"%s",yr)>0)
{
printf("%s\n",yr);
index=0;
}
printf("Enter year: ");
scanf("%s",input_yr);
printf("\n\n");
rewind(sy);
while(fscanf(sy,"%s",yr)>0)
{
if(strcmpi(input_yr,yr)==0)
{
strcat(yr,".txt");
fp1=fopen(yr,"r");
while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
{
printf("%s\t%s\t%s\n",sn,fn,ln);
}
printf("\n\n");p("Enter Student no.: ");
scanf("%s",srch);
system("cls");
rewind(fp1);
while(fscanf(fp1,"%s\t%s\t%s",sn,fn,ln)>0)
{
if(strcmpi(srch,sn)==0)
{
sprintf(flnme,"%s/%s.txt",yr,sn);
fp2=fopen(flnme,"r");
while(fgets(line,sizeof(line),fp2) !=NULL)
{
printf("%s",line);
}
fclose(fp2);
}
}
}
fclose(fp1);
}
fclose(sy);
getch();
}
thank you and more power!!

New Topic/Question
Reply



MultiQuote







|