Why is the last input printed twice in the text.txt?

How to create a c program wer on giving a input details regarding that input is displayed on screen ?

  • i have file in which details are stored as id no name father's name section when i give id no as input...details regarding that id should be printed on the output screen...to be specific only that line has to be printed...help me guys !

  • Answer:

    include<stdio.h> #include<stdlib.h> int main() { FILE *IFL; int n,id; char x[200], name[20], fname[20]; IFL=fopen("xyz.dat", "r"); printf("Enter Student id\n"); scanf("%d", &n); while(!feof(IFL)) { fgets(x,200, IFL); sscanf(x,"%d%s%s", &id, name,fname); if(n==id){ printf("%s\n", x); break;} } fclose(IFL); system("PAUSE"); return 0; } The xyz.dat file contains 110 Ram Ravi 111 Rinu Renu 123 Abhi Anuj 119 Resma Grishma

rinu at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

you could just use grep or DOS equivalents, but if you must use C, you will need to read the whole file in, find all the newlines to split everything into single lines in an array of char arrays. The tokenise each line into an array of structs. Then search your array for a matching entry, then send the matching line string to the standard output. You could make things more efficient by doing the tokenisation and search as you do the read.

Alex

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.