Project help

+1 Suraj Gautam · August 23, 2015
hello friends, I am currently building my C project for my college. I am making student management system. 
How can I search the student with his ID?
I created a file with details about the student and now i want to search and display the details of the student by searching with Id number,
GIve me a clue.

Post a Reply


Oldest  Newest  Rating
0 Jon Z. · September 9, 2015
try using a 3D char array (2D string array) and follow this structure.
this is pseudocode. of course in c you can't assign strings this way.
char students[# of students][# of lines per student][20]
int i = 0;
int j = 0;
while(is not the end of the file){
 chat tmp[20];
 tmp = line from file;
 if(tmp[0] == '\n'){
   j= 0;
   continue; //this goes back to the beginning of the loop
 students[i][j] = line from file;

now the array students contains all the students' info. now you can loop through it. the first index (i) is the student, and the second (j) the info of the student.
so students[0][0] is the id of the first student, students[0][1] is the name of the first student, students[1][0] is the id of the second student, and so on...
0 c student · August 28, 2015

123 Z, Jon USA Bachelor Management
334 Nepal, Suraj Bachelor Management


char buf[SIZE];
FILE *fp = fopen ("file.dat", "r");

while (fgets (buf, sizeof (buf), fp) != NULL) {
if (strstr (buf, "123") != NULL) {
printf ("ID: 123 found!\n");
0 Florin Nastase · August 28, 2015
I suggest you to read line by line and when you find the ID you're looking for, read the next lines until you find an empty line.
0 Franz Schmidt · August 27, 2015
I'm not a specialist at filebased programs but as far as I know you are able to read in a specific line of a file (e.g.: line 3) and you get a string with everything whats in that line. 

So if you want to search by ID and the structure of your file is like in your post then you only need to read in every 7th line (0,7,14,...) and when the id of the file matches the entered id you want to find you've got your student.

I'm not sure what you use for filehandling but I expect something like fstream or fopen. And if I'm not wrong you can jump with fseek or fget to to specific line or position in your file. Please correct me if I'm wrong.:ermm:
0 Suraj Gautam · August 27, 2015
my file looks like
123 /*ID*/

If I have 2 contents in my file, then i have to display the contents with the help of ID. If user pressed 123 then it must show all the details of JONZ. How can i do this?
0 Jon Z. · August 26, 2015
let's say your file looks like this 
1, john
2, bob
3, jane

then you can load every line into an array of strings and then check if the first char equals whatever.

you can also use the atoi function to convert strings into integers.
0 Suraj Gautam · August 25, 2015
ok friend, i will try
0 c student · August 23, 2015
how are the details in your file formatted?  perhaps you could read in entire lines and search for a substring of the student's id?
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator