Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating Student Information system in C language.
the Structure is
struct { char firstname[20];
char pass[20];
}student;



Print the String in File with Below Format
C++
fprintf(fp,"%d\t\t%s\t\t P\t\t%d:%d:%d\t\t\n",index,student.firstname,t.ti_hour,t.ti_min,t.ti_sec);


<pre lang="cs">void searchname()
{  int index;
found=0;
fp=fopen("attdb.txt","r");
if(fp == NULL)
    {
     outtextxy(100,450,"! The File is Empty...\n\n");
     }
      else
    {
    outtextxy(100,300,"\nEnter student first name:");
     scanf("%s",&studentname);
    do
    {      
    fscanf(fp,"%d\t\t%s",index,student.firstname);
    length = strlen(student.firstname);
    if(student.firstname[length]==studentname[length])
    found=1;
    }while(!feof(fp));
}
 if(found==1)
 {
    outtextxy(100,400,"\nThe record is found.");
    getch();
  }
 else
 {
     outtextxy(100,400,";Not found...\n");

 }



its is gone in infinite loop...
Posted

I would change

C++
do
{
fscanf(fp,"%d\t\t%s",index,student.firstname);
length = strlen(student.firstname);
if(student.firstname[length]==studentname[length])
found=1;
}while(!feof(fp));


to

C++
while !feof(fp)
{
    fscanf(fp,"%d\t\t%s",index,student.firstname);
    length = strlen(student.firstname);
    if(student.firstname[length]==studentname[length])
        break;
}
 
Share this answer
 
C++
   if(student.firstname[length]==studentname[length])
    found=1;
    }while(!feof(fp));
}

This is not going to work, you need to use a proper string comparison function to check if the names match. You also do not need the ampersand & On the variable name when scanning a string input field.
 
Share this answer
 
C#
fp=fopen("attdb.txt","r");
if(fp == NULL)    <=== try changing == to != 

:-0 My apologies, my eyes did not see the formatting correctly. :-0
 
Share this answer
 
v2
Comments
Parth Akbari 27-Jun-14 12:36pm    
no .... its not work...its gives "The File is Empty.." and "Not found..." message...
You might want to include || found != 1 in your while loop condition.

Good luck!
 
Share this answer
 
Comments
Parth Akbari 27-Jun-14 4:12am    
i Add in my loop but still going in infinite loop.while((!feof(fp)) || (found!=1));
is there any mistake in fscanf "\t" may problem with reading.
HERE is my TXT file structure "http://s28.postimg.org/43dqgfafx/screenshot_2014_06_27.jpg"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900