reading data file

c programing file.

Page 1 of 1

13 Replies - 7056 Views - Last Post: 07 August 2006 - 03:11 PM Rate Topic: -----

#1 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

reading data file

Post icon  Posted 04 August 2006 - 07:05 AM

I need help to get this program to read the file name from a user and then read and display the data.

#include <stdio.h>
#include <string.h>  //for string comparisons

  #define LISTSIZE 100 //max number of students in the array
  #define flatfeeone 500.00	/*flat rate for up to 6 credits*/
  #define Flatfeeu  450.00	/*rate for undergrad rate for 6 to 12 credits*/
  #define Flatfeeg  750.00	/*rate for graduate for 6 to 12 credits*/
  #define flatunit  250.00	/*rate for fee per credit up to 6 credits*/
  #define Flatunitu  245.00	/*rate for undergrate for 6-12 credits*/
  #define Flatunitg  285.00	/*rate for graduate for 6-12 credits*/
  #define Flatpuu	270.00	/*rate for undgrade for 12-18 credits*/
  #define Flatgu	 340.00	/*rate for graduate for 12-18 credit*/
  #define Flatu	 5100.00	/*rate for undgrade for 18 or more credits*/
  #define Flatg	 6400.00	/*rate for graduate for 18 or more credits*/
  #define LINELEN 80 //Possible length of input line
  
  
typedef struct info {  //this is the student infomation
	int ssnumber; //social security number
	char status;	/*Student status (G,g-Grad or U,u-Undergrad)*/
	float gpa;  //students gpa
	int credits; //students credits 
} Info_T;

Info_T Iinlist[LISTSIZE];   //list of stundernts info


/******************************************************************************/ 

main ()
{
/*function used by main only*/
	int Inforread (Info_T*);
	void PrintINfo (Info_T*);
	int Numinf,   //Number of students in array
		CurrInf;  //Index of current student
	float cost;   /*amount of tutition*/
	Info_T *MatchInf; //Pointer to clinet
	
	Numinf = Inforread (Iinlist); //*pass address of 1st elemt of array
	
}
	
  //read all client from file into array
	int Inforread (Info_T *Iinlist)
	{ 
		char line[LINELEN+1]; //Line read from file
		int Fileok;		  //Flag indicated file open for reading ok
		char out;			//throw away a charter
		FILE *data;		  //Data file pointer
		char filename[LINELEN];//Data file name
		Info_T *Curr;		//Pointer to current student (student being read)
		int Currnum = 0;  //Index into array for student
		
		printf ("\nEnter data filename:  ");
		scanf ("%s", filename);
		data = fopen (filename, "r");

	if (data == NULL)  //error checking the file is there
	{
		printf ("Error openeing file\n");
	}		

 
		while (fgets (line, LINELEN+1, data) != NULL) //read each client's data
		{
			  Curr = &Iinlist[Currnum];
				
			  fscanf (data, "%s %c %f %d", Curr->ssnumber, &(Curr->status), Curr->gpa, 
				  &(Curr->credits), &out);
			  
			  fprintf (data, "%s %c %f %d", Curr->ssnumber, &(Curr->status), Curr->gpa, 
				  &(Curr->credits), &out);
			  Currnum++;
			  }
	fclose (data);
	return Currnum;
}



Is This A Good Question/Topic? 0
  • +

Replies To: reading data file

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: reading data file

Posted 04 August 2006 - 07:06 AM

Can you specify the problem? Are you receiving any error messages?
Was This Post Helpful? 0
  • +
  • -

#3 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: reading data file

Posted 04 August 2006 - 07:09 AM

when I run the program I enter in the file name. And it just sits there. I'm not able to see if it is even got to the file. Other times a I get a windows error message and it ask to send report I say no and the dos window closes.
Was This Post Helpful? 0
  • +
  • -

#4 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: reading data file

Posted 04 August 2006 - 07:26 AM

first of all be sure that the file exists. you have added the if it doesnt exist but you are not exiting the function after that. just add return statement returning a number say -1 and check in the main function if you have a valid index or not and perform the displaying function accordingly
Was This Post Helpful? 0
  • +
  • -

#5 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: reading data file

Posted 04 August 2006 - 07:29 AM

yes the file is there.

what do you mean by check in the main function if you have a valid index or not and perform the displaying function accordingly
Was This Post Helpful? 0
  • +
  • -

#6 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: reading data file

Posted 04 August 2006 - 07:54 AM

  if (data == NULL)  //error checking the file is there
	{
		printf ("Error openeing file\n");
	}		



here is your code for checking if the file exists or not.now if the file exists then let the function continue but if doesnt then you should quit the function .now since you are returning an integer from the function you would need to return at this point from the function. what i was suggesting was that you may want to return an invalid index like -1. check if the function returns a valid index or not and like wise display it.

also i guess u are getting a blank screen because you are not displayng the data you are retrieving
Was This Post Helpful? 0
  • +
  • -

#7 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: reading data file

Posted 04 August 2006 - 08:28 AM

yeah i think that I am having issues getting the data. If i wanted to set this up how would i do that invalid index like -1
Was This Post Helpful? 0
  • +
  • -

#8 frog  Icon User is offline

  • unleashed
  • member icon

Reputation: 2
  • View blog
  • Posts: 683
  • Joined: 26-March 06

Re: reading data file

Posted 04 August 2006 - 11:44 AM

what i meant by an invalid index was simply a reference that you could keep to check for a certain condition.now you are defining an array of 100 elements.therefore the index can be only between 0 to 99 . you would know the function would never return 100 or -1 or anything else for that matter as an answer(that would be the case if the record is not found or if file doesnt exist). now you can return -1 if file doesnt exist after the code written above or you could return something like 101 for not finding the data. you could check for that condition and then display a message accordingly
Was This Post Helpful? 0
  • +
  • -

#9 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: reading data file

Posted 04 August 2006 - 12:54 PM

View Postfrog, on 4 Aug, 2006 - 11:44 AM, said:

what i meant by an invalid index was simply a reference that you could keep to check for a certain condition.now you are defining an array of 100 elements.therefore the index can be only between 0 to 99 . you would know the function would never return 100 or -1 or anything else for that matter as an answer(that would be the case if the record is not found or if file doesnt exist). now you can return -1 if file doesnt exist after the code written above or you could return something like 101 for not finding the data. you could check for that condition and then display a message accordingly



ok i will try that
Was This Post Helpful? 0
  • +
  • -

#10 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: reading data file

Posted 06 August 2006 - 12:52 PM

Ok i can get it to open the file. I need help to get it to read the file and display it to the screen. When I tun the program i get to the point where it say at line 5 and then the dos window closes. Not to sure hwat I'm doing worng and need some help. I'm trying ot use a structures of arry to read the data. That is if i even have that part of the program correct. help please

/*****************************************
File Name: Tmitchellwk3
Description:This program will read a file that contain a students social security numeber
GPA, class status and number of credits. It will read all of the data from the file and 
put it into a array of structures. while in a loop. 
Date: 8/2/06
Desinger: Tabatha Mithcell
Functions:  Input, output
*****************************************/
#include <stdio.h>
#include <string.h>  //for string comparisons

  #define LISTSIZE 100 //max number of students in the array
  #define flatfeeone 500.00	/*flat rate for up to 6 credits*/
  #define Flatfeeu  450.00	/*rate for undergrad rate for 6 to 12 credits*/
  #define Flatfeeg  750.00	/*rate for graduate for 6 to 12 credits*/
  #define flatunit  250.00	/*rate for fee per credit up to 6 credits*/
  #define Flatunitu  245.00	/*rate for undergrate for 6-12 credits*/
  #define Flatunitg  285.00	/*rate for graduate for 6-12 credits*/
  #define Flatpuu	270.00	/*rate for undgrade for 12-18 credits*/
  #define Flatgu	 340.00	/*rate for graduate for 12-18 credit*/
  #define Flatu	 5100.00	/*rate for undgrade for 18 or more credits*/
  #define Flatg	 6400.00	/*rate for graduate for 18 or more credits*/
  #define LINELEN 80 //Possible length of input line
  #define SSsize 11 //size of the ss number
  
typedef struct Info {  //this is the student infomation
	int ssnumber[SSsize+1]; //social security number
	char status;	/*Student status (G,g-Grad or U,u-Undergrad)*/
	float gpa;  //students gpa
	int credits; //students credits 
} Info_T;

Info_T Iinlist[LISTSIZE];   //list of stundernts info


/******************************************************************************/ 

main ()
{
/*function used by main only*/
	int Inforread (Info_T*);
	void PrintInfo (Info_T*);
	
	int Numinf,   //Number of students in array
		CurrInf;  //Index of current student
	char yn;   // Yes/no use input
	float cost;   /*amount of tutition*/
	Info_T *MatchInf; //Pointer to clinet
	Numinf = Inforread (Iinlist); //*pass address of 1st elemt of array
	
}
	
  //read all client from file into array
	int Inforread (Info_T *InfList)
	{ 
		char line[LINELEN+1]; //Line read from file
		int Fileok;		  //Flag indicated file open for reading ok
		char out;			//throw away a charter
		FILE *data;		  //Data file pointer
		char filename[LINELEN];//Data file name
		Info_T *Curr;		//Pointer to current student (student being read)
		int CurrNum = 0;  //Index into array for student
		
		printf ("\nEnter data filename:  ");
		scanf ("%s", filename);
		data = fopen (filename, "r");
		printf ("\nat Line 2");

	if (data == NULL)  //error checking the file is there
	{
		printf ("Error openeing file\n");
	}		
  printf ("\nat Line3");
		while (fgets (line, LINELEN+1, data) != NULL) //read each client's data
		{
		   printf ("\nat line 4");
			  Curr = &Iinlist[CurrNum];
		   printf ("\nat line 5");   
			  sscanf (line, "%s %c %f %d", Curr->ssnumber, &(Curr->status), Curr->gpa, 
				  &(Curr->credits), &out);
		printf ("\nat line 8");
			  fscanf (data, "%s %c %f %d", Curr->ssnumber, &(Curr->status), Curr->gpa, 
				  &(Curr->credits), &out);
		   printf ("\nat line 6");
			  fprintf (data, "%s %c %f %d", Curr->ssnumber, &(Curr->status), Curr->gpa, 
				  &(Curr->credits), &out);
		   printf ("\nat line 7");
			  CurrNum++;
			  }
	fclose (data);
	return CurrNum;
}



 
	 






View Postmitchelltab, on 4 Aug, 2006 - 12:54 PM, said:

View Postfrog, on 4 Aug, 2006 - 11:44 AM, said:

what i meant by an invalid index was simply a reference that you could keep to check for a certain condition.now you are defining an array of 100 elements.therefore the index can be only between 0 to 99 . you would know the function would never return 100 or -1 or anything else for that matter as an answer(that would be the case if the record is not found or if file doesnt exist). now you can return -1 if file doesnt exist after the code written above or you could return something like 101 for not finding the data. you could check for that condition and then display a message accordingly



ok i will try that

Was This Post Helpful? 0
  • +
  • -

#11 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: reading data file

Posted 06 August 2006 - 02:00 PM

How did you write the information to the file to begin with?

Did you write it using Structures or just opened a text editor and typed it in?

If you created the file with Structures then use fread(void *ptr, size_t size, size_t nmemb, FILE *stream);.

If you created the file with a text editor then you are going to have to read an entire line at a time and split the data apart in order to put it into your Structure.
Was This Post Helpful? 0
  • +
  • -

#12 mitchelltab  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 13-July 06

Re: reading data file

Post icon  Posted 07 August 2006 - 10:29 AM

I used it as a txt file. That I created. so it was not created with a structure. I guess that is where I'm having my issues. I can get it to read the first line of the file only. The the code stops. In the test edior they are all on the same line. So for each item on the line i need a printf and scanf for each item. I have it set up to where it reads the first line in the txt then stops here is the new code. Let me know how to correct this so that i'm able to read more then just the frst line.

 
/*****************************************
File Name: Tmitchellwk3
Description:This program will read a file that contain a students social security numeber
GPA, class status and number of credits. It will read all of the data from the file and
put it into a array of structures. while in a loop.
Date: 8/2/06
Desinger: Tabatha Mithcell
Functions:  Input, output
*****************************************/
#include <stdio.h>
#include <string.h>  //for string comparisons

  #define LISTSIZE 100 //max number of students in the array
  #define flatfeeone 500.00	/*flat rate for up to 6 credits*/
  #define Flatfeeu  450.00	/*rate for undergrad rate for 6 to 12 credits*/
  #define Flatfeeg  750.00	/*rate for graduate for 6 to 12 credits*/
  #define flatunit  250.00	/*rate for fee per credit up to 6 credits*/
  #define Flatunitu  245.00	/*rate for undergrate for 6-12 credits*/
  #define Flatunitg  285.00	/*rate for graduate for 6-12 credits*/
  #define Flatpuu	270.00	/*rate for undgrade for 12-18 credits*/
  #define Flatgu	 340.00	/*rate for graduate for 12-18 credit*/
  #define Flatu	 5100.00	/*rate for undgrade for 18 or more credits*/
  #define Flatg	 6400.00	/*rate for graduate for 18 or more credits*/
  #define LINELEN 80 //Possible length of input line


typedef struct info {  //this is the student infomation
	int ssnumber[11]; //social security number
	char status;	/*Student status (G,g-Grad or U,u-Undergrad)*/
	float gpa;  //students gpa
	int credits; //students credits
} Info_T;

Info_T Iinlist[LISTSIZE];   //list of stundernts info


/******************************************************************************/

main ()
{
/*function used by main only*/

	int Inforread (Info_T*);
	void PrintINfo (Info_T*);
	int Numinf,   //Number of students in array
		CurrInf;  //Index of current student
	float cost;   /*amount of tutition*/
	Info_T *MatchInf; //Pointer to clinet

	Numinf = Inforread (Iinlist); //*pass address of 1st elemt of array

}

  //read all client from file into array
	int Inforread (Info_T *Iinlist)
	{
		char line[LINELEN+1]; //Line read from file
		int Fileok;		  //Flag indicated file open for reading ok
		char out;			//throw away a charter
		FILE *data;		  //Data file pointer
		char filename[LINELEN];//Data file name
		Info_T *Curr;		//Pointer to current student (student being read)
		int Currnum = 0;  //Index into array for student
		 int ssnumber; //social security number
		char status;	/*Student status (G,g-Grad or U,u-Undergrad)*/
		float gpa;  //students gpa
		int credits; 


		printf ("\nEnter data filename:  ");
		scanf ("%s", filename);
		data = fopen (filename, "r");
		printf("at line 1\n");


	if (data == NULL)  //error checking the file is there
	{
		printf ("Error openeing file\n");
	}

   printf ("at line 2\n");\

   char *returnCode = fgets (line, LINELEN, data);  // start reading the file.

   printf ("got a line read");

   printf ("read: %s\n", line);

		 while (returnCode != NULL) //read each client's data
		{
			  printf ("\ninside while read: %s", line);
			  //Curr = &Iinlist[Currnum];
			  printf ("at line 4\n");
			  fscanf (data, "%s%c%f%d", &ssnumber, &status, &gpa, &credits);
			  printf ("%s %c %f %d", ssnumber, status, gpa, credits);

			  //printf("at line 2\n");
			  Currnum++;

			  returnCode = fgets(line, LINELEN, data);
			  return 0;
		  }
	fclose (data);
	printf("at line 3");
	return Currnum;
	
	return 0;
	return 0;
}



View Postjayman9, on 6 Aug, 2006 - 02:00 PM, said:

How did you write the information to the file to begin with?

Did you write it using Structures or just opened a text editor and typed it in?

If you created the file with Structures then use fread(void *ptr, size_t size, size_t nmemb, FILE *stream);.

If you created the file with a text editor then you are going to have to read an entire line at a time and split the data apart in order to put it into your Structure.

Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: reading data file

Posted 07 August 2006 - 02:58 PM

View Postmitchelltab, on 7 Aug, 2006 - 10:29 AM, said:

Let me know how to correct this so that i'm able to read more then just the frst line.


Put the code that reads each line and outputs the results in a WHILE loop.

When a file stream has reached the end of a file, it returns a value that you can check using the feof function. You give function feof the file stream, which in your case is called data.

Now it will loop through the entire file until it reaches the end.

	while(!feof(data))
	{		
		//your code goes here	   
	}


Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: reading data file

Posted 07 August 2006 - 03:11 PM

You have some extra returns inside your function Inforread. You need to remove 3 of them, you only need one in this function. That is probably why your function is ending prematurely.
			  returnCode = fgets(line, LINELEN, data);
			  return 0;
		  }
	fclose (data);
	printf("at line 3");
	return Currnum;
	
	return 0;
	return 0;
}



I suggest:
			  returnCode = fgets(line, LINELEN, data);			 
		  }
	fclose (data);
	printf("at line 3");

	return Currnum;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1