Thread: reading file in turbo c

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    reading file in turbo c

    Code:
    sir, maam can you help me again with my program, this is my code: 
    
     #include<string.h>
     #include<stdio.h>
      char code1[8],name[20],code[8];
      int level;
    
     main()
     {
     FILE *fp;
     fp=fopen("emp.txt","r");
    
      clrscr();
      printf("Enter code: ");
      gets (code1);
    
     do{
        fscanf(fp,"%s %s %d",name,code,&level);
      if (strcmpi(code,code1)==0)
       {
        printf("employee name:%s\n",name);
        printf("employee code:%s\n",code);
        printf("employee salary level: %d\n",level);
       }
      }while(!feof(fp));
     fclose(fp);
    
    
     getche();
     }
    
    content of emp.txt:
    juan_dela_cruz,A02-0001,2
    mary_palm,A02-0003,1
    pedro_santos,A02-0005,3
    
    the problem is that even if i type the correct employee code, it doesn't display the employees profile...
    pls.... help me. tnx a lot and more power to this site

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Ok here are a few points:
    1. Give space(remove commas) in your file data for fscanf to read it properly like this-
    juan_dela_cruz A02-0001 2
    mary_palm A02-0003 1
    pedro_santos A02-0005 3
    2. clrscr() is in the header conio.h, so include it
    3. Dont use gets ever. Use fgets instead.
    4. Better if you avoid using TC, coz its outdated.
    5. Use code tags only for posting codes.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you print out code and code1, you can see they are exactly the same, except for their case, *right* ?

    No newlines or end of string char's in one code, and not in the code1 you're trying to match up?

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    turbo c

    thanks..... I try it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM