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
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:
- How to Convert a C++ Structure into C# Structure?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- How to pass a C structure in Python?Best solution by Stack Overflow
- How to create a simple counter program?Best solution by Stack Overflow
- How to create this Java program?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.