Searching a data from txt file...

serching a name from txt data...

Page 1 of 1

6 Replies - 2907 Views - Last Post: 27 August 2009 - 03:00 AM Rate Topic: -----

#1 556313   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 03-November 08

Searching a data from txt file...

Posted 27 August 2009 - 02:01 AM

why i only can search the first person and other person i cant search it....
what's wrong for my coding???
Who can help me? Thanks

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

typedef struct 
{
	char studName[20];
	int studID[7];
	char sex;
	char email[40];
	int mathScore;
	int scienceScore;
	int engScore;
}DATA;

int main ()	   
{
	int k, numStud = 0;
	char name[20];
	FILE* spINPUT;
	DATA data [50];

	if ((spINPUT = fopen ("inputfile.txt", "r")) == NULL)
	{
		printf("Sorry, file does not exist or file cant open!\n");
		printf("Please create your file or try again!!!\n");
		exit(0);
	}

	do
	{
		fscanf(spINPUT,"%s ", &data[numStud].studName);
		fscanf(spINPUT,"%s ", &data[numStud].studID);
		fscanf(spINPUT,"%s ", &data[numStud].sex);
		fscanf(spINPUT,"%s ", &data[numStud].email);
		fscanf(spINPUT,"%d ", &data[numStud].mathScore);
		fscanf(spINPUT,"%d ", &data[numStud].scienceScore);
		fscanf(spINPUT,"%d ", &data[numStud].engScore);

		numStud++;
	}while(getc(spINPUT)!=EOF);

	printf("Searching student data...\n");
	printf("==========================\n");
	printf("\nPlease enter the NAME of student that you want to search...\n");
	scanf(" %[^\n]", &name);

		for(int i = 0; i <= numStud; i++)
	{
		if(strcmp(name , data[i].studName) == 0)
		{
			k = i;
		}
	}

	printf("\n\n--------------------\n");
	printf("\nStudent Name : \t\t%s\n",data[k].studName);
	printf("Student ID is :  %13s\n",data[k].studID);
	printf("Sex :  %18c\n",data[k].sex);
	printf("Email : \t\t%s\n",data[k].email);
	printf("Mathematic score :  %6d\n",data[k].mathScore);
	printf("Science score :  %9d\n",data[k].scienceScore);
	printf("English score :  %9d\n",data[k].engScore);
	printf("--------------------\n");

	fclose (spINPUT);

	return 0;
}



This a inputfile.txt


Michael
000000
m
[email protected]
74
82
90
Tammy
000001
f
[email protected]
85
77
92
John
000002
m
[email protected]
90
64
75
Alex
000003
m
[email protected]
88
93
74


Is This A Good Question/Topic? 0
  • +

Replies To: Searching a data from txt file...

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Searching a data from txt file...

Posted 27 August 2009 - 02:13 AM

View Post556313, on 27 Aug, 2009 - 08:01 AM, said:

why i only can search the first person and other person i cant search it....
what's wrong for my coding???
Who can help me? Thanks

after reading information you should always print it out so you can check you have got what you expected
if I print you data[] array
   do
	{
		fscanf(spINPUT,"%s ", &data[numStud].studName);
		fscanf(spINPUT,"%s ", &data[numStud].studID);
		fscanf(spINPUT,"%s ", &data[numStud].sex);
		fscanf(spINPUT,"%s ", &data[numStud].email);
		fscanf(spINPUT,"%d ", &data[numStud].mathScore);
		fscanf(spINPUT,"%d ", &data[numStud].scienceScore);
		fscanf(spINPUT,"%d ", &data[numStud].engScore);

		numStud++;
	}while(getc(spINPUT)!=EOF);

		for(int i = 0; i <= numStud; i++)
		   printf("%s\n", data[i].studName );



I get
Michael
ammy
ohn
lex
	‘|5
Searching student data...
==========================

--------------------

showing you are not getting what you expected so when you search it fails

This post has been edited by horace: 27 August 2009 - 02:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 556313   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 03-November 08

Re: Searching a data from txt file...

Posted 27 August 2009 - 02:26 AM

View Posthorace, on 27 Aug, 2009 - 01:13 AM, said:

View Post556313, on 27 Aug, 2009 - 08:01 AM, said:

why i only can search the first person and other person i cant search it....
what's wrong for my coding???
Who can help me? Thanks

after reading information you should always print it out so you can check you have got what you expected
if I print you data[] array
   do
	{
		fscanf(spINPUT,"%s ", &data[numStud].studName);
		fscanf(spINPUT,"%s ", &data[numStud].studID);
		fscanf(spINPUT,"%s ", &data[numStud].sex);
		fscanf(spINPUT,"%s ", &data[numStud].email);
		fscanf(spINPUT,"%d ", &data[numStud].mathScore);
		fscanf(spINPUT,"%d ", &data[numStud].scienceScore);
		fscanf(spINPUT,"%d ", &data[numStud].engScore);

		numStud++;
	}while(getc(spINPUT)!=EOF);

		for(int i = 0; i <= numStud; i++)
		   printf("%s\n", data[i].studName );



I get
Michael
ammy
ohn
lex
	|5
Searching student data...
==========================

--------------------

showing you are not getting what you expected so when you search it fails




So how can solve my problem
Was This Post Helpful? 0
  • +
  • -

#4 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Searching a data from txt file...

Posted 27 August 2009 - 02:28 AM

View Post556313, on 27 Aug, 2009 - 08:26 AM, said:

So how can solve my problem

try removing the spaces after the %s in lines such as
		fscanf(spINPUT,"%s ", &data[numStud].studName);


This post has been edited by horace: 27 August 2009 - 02:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 joske   User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: Searching a data from txt file...

Posted 27 August 2009 - 02:37 AM

Here this works better.

I added a line to visualize what data is loaded.

I also changed the fscanf commands slightly, and changed your data type int studId[7] to char studId[7]

There was an other error in readin the end of file (you used getc which did "eat" the first letter of the names). This is better:

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

typedef struct
{
	char studName[20];
	char studID[7];
	char sex;
	char email[40];
	int mathScore;
	int scienceScore;
	int engScore;
}DATA;

int main ()
{
	int k = 0, numStud = 0;
	char name[20];
	FILE* spINPUT;
	DATA data [50];

	if ((spINPUT = fopen ("inputfile.txt", "r")) == NULL)
	{
		printf("Sorry, file does not exist or file cant open!\n");
		printf("Please create your file or try again!!!\n");
		exit(0);
	}

	printf("Loading student data...\n");
	while (!feof(spINPUT))
	{
		fscanf(spINPUT,"%s ", data[numStud].studName);
		fscanf(spINPUT,"%s ", data[numStud].studID);
		fscanf(spINPUT,"%c ", &data[numStud].sex);
		fscanf(spINPUT,"%s ", data[numStud].email);
		fscanf(spINPUT,"%d ", &data[numStud].mathScore);
		fscanf(spINPUT,"%d ", &data[numStud].scienceScore);
		fscanf(spINPUT,"%d ", &data[numStud].engScore);

		printf("data[%i]=%s, %s, %c, %s, %d, %d, %d\n",
		  numStud,
		  data[numStud].studName,
		  data[numStud].studID,
		  data[numStud].sex,
		  data[numStud].email,
		  data[numStud].mathScore,
		  data[numStud].scienceScore,
		  data[numStud].engScore);

		numStud++;
	}

	printf("Student data loaded (%i students).\n\n", numStud);

	printf("Searching student data...\n");
	printf("==========================\n");
	printf("\nPlease enter the NAME of student that you want to search...\n");
	scanf(" %[^\n]", &name);

		for(int i = 0; i < numStud; i++)
	{
		if(strcmp(name , data[i].studName) == 0)
		{
			k = i;
		}
	}

	printf("\n\n--------------------\n");
	printf("\nStudent Name : \t\t%s\n",data[k].studName);
	printf("Student ID is :  %13s\n",data[k].studID);
	printf("Sex :  %18c\n",data[k].sex);
	printf("Email : \t\t%s\n",data[k].email);
	printf("Mathematic score :  %6d\n",data[k].mathScore);
	printf("Science score :  %9d\n",data[k].scienceScore);
	printf("English score :  %9d\n",data[k].engScore);
	printf("--------------------\n");

	fclose (spINPUT);

	return 0;
}



(Note also that searching a name is case sensitive)

This post has been edited by joske: 27 August 2009 - 02:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 556313   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 03-November 08

Re: Searching a data from txt file...

Posted 27 August 2009 - 02:43 AM

Thanks horace and joske. I already solve the problem. THanks a lot.

But if i want do a function to delete a data that record in the inputfile.txt e.g Alex's record. How can i do it. Is it posible to do it? I no idea to do it...
Was This Post Helpful? 0
  • +
  • -

#7 joske   User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: Searching a data from txt file...

Posted 27 August 2009 - 03:00 AM

I should write a couple of functions:
- loadData(), which loads the data from the file
- saveData(), which saves the data currently in memory to the file (overwriting old data)
- addRecord(), which adds a record to your data[] array.
- removeRecord(int recordNum), which removes a record from your data[] array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1