--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Please help me...
Sir/Ma'am I think I got this one working using this code but I have one problem:
this is the code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char code[80],code2[80], ename[80];
int elevel;
clrscr();
if((fp = fopen("samplete.txt","r"))==NULL) {
printf("cannot open file.\n");
}

printf("Enter ID: ");
gets(code2);

 while(!feof(fp)) {

    fscanf(fp,"%s %s %d",code,ename,&elevel);

        if(strcmp(code2,code)) {
           printf("%s %s %d",code,ename,elevel);
           break;
           }
 }
fclose(fp);
getch();
}

The problem is it only display the first record or first line of text in my notepad file

lets say when I Enter the ID: ST-0002

it only display the first record or the first line of text file which is: ST-0001, Eddie_VanHalen, 1

it should be: ST-0002, James_Hatfield, 2

Please help me... thanks in advanced

Dani AI

Generated

Quick diagnosis: the code reads the file and then enters the if when the strings differ, so the first line is printed and the loop breaks. That explains why entering a different ID still shows the first record. Other problems to address at the same time are unsafe input (use of gets), using while(!feof(...)), and not checking fopen failure. is correct to call those out.

A safer, simpler approach is to read the file line-by-line with fgets, parse each line (CSV-style if the file uses commas) with sscanf or a scanset, trim the newline from the user input, then compare with strcmp(...) == 0. Check all return values and use int main(void).

Example (compact, robust pattern):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    FILE *fp = fopen("samplete.txt", "r");
    if (!fp) { perror("samplete.txt"); return 1; }

    char line[256], search[64], id[64], name[128];
    int level, found = 0;

    printf("Enter ID: ");
    if (!fgets(search, sizeof search, stdin)) { fclose(fp); return 1; }
    search[strcspn(search, "\r\n")] = '\0'; /* strip newline */

    while (fgets(line, sizeof line, fp)) {
        if (sscanf(line, " %63[^,], %127[^,], %d", id, name, &level) == 3) {
            id[strcspn(id, " \t\r\n")] = '\0';
            if (strcmp(search, id) == 0) {
                printf("%s, %s, %d\n", id, name, level);
                found = 1;
                break;
            }
        }
    }

    if (!found) printf("ID not found.\n");
    fclose(fp);
    return 0;
}

Key points to keep in mind: remove the trailing newline from the search string, parse the file reliably rather than tokenizing on whitespace, use strcmp(...) == 0 for equality, and always check return values (file open, fgets, sscanf). These changes eliminate the “first-line always” symptom and avoid common runtime hazards.

...but I have one problem

No, you have many problems.
1) Using bold, underline, red text, lines of hyphens to make your post look cute. It's unnecessary and distracting. By the way, you forgot to use italics.
2) Your formatting is bad -- see this
3) void main() -- main() is never a void, see this
4) clrscr(); -- useless and annoying for the user, and hasn;t been available in compilers since 1990
5)

if((fp = fopen("samplete.txt","r"))==NULL) {
printf("cannot open file.\n");
}

What happens if it can't open the file? You continue anyway
6) gets(code2); -- extremely dangerous, see this
7) while(!feof(fp)) -- error prone loop, see this

You might want to learn from a better source. Your current source seems to be older than the hills, and is teaching very bad techniques.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.