|
 |
|
 |
04-18-2003, 12:08 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2003
Posts: 2
|
Newbie question.
I'm completely new here (19 year old webgeek taking her first dive into C at Mount Holyoke College), so play nice =)
So I'm reading a text file created in the first program I had to write into a second program I'm attempting to finish. What I need to do is give the average income, weight and age, as well as the names of any people whose age is above the average. That's fine. But what I'm stuck on is how exactly to get a count of the total number of people that I have information for, so I have a number to divide by in order to get the averages. Yes, this is a homework assignment - I'm not asking for it to be done for me, I just need a clue on how to do it. Any help I can get would be wonderful.
Code:
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[25];
char ssn[12];
char city[50];
int age;
int weight;
char eyes[5];
double income;
} people_t;
int main (void)
{
int i=0;
int temp;
people_t x[25];
FILE *pFile;
if (( pFile = fopen("data.dat", "r")) == NULL)
printf("\nCannot open file\n");
for (i = 0; (i < 25); i++)
{
fscanf(pFile,"%[^\n]\n", &x[i].name);
fflush (stdin);
fscanf(pFile,"%[^\n]\n", &x[i].ssn);
fflush (stdin);
fscanf(pFile,"%[^\n]\n", &x[i].city);
fflush (stdin);
fscanf (pFile, "%d", &x[i].age);
fflush (stdin);
fscanf (pFile, "%d", &x[i].weight);
fflush (stdin);
fscanf(pFile,"%s", &x[i].eyes);
fflush (stdin);
fscanf (pFile, "%lf\n", &x[i].income);
fflush (stdin);
}
fclose(pFile);
return 0;
}
Any help is greatly appreciated.
|
|
|
04-18-2003, 01:18 AM
|
#2 (permalink)
|
|
bloomberg
Join Date: Jun 2002
Location: bloomberg
Posts: 263
|
... seems to me you're reading in data for 25 people no?
so ... 25?
i guess the answer is how are your "people" stored
1 per 3 lines?
.........then count total lines, div by 3 ...
no?
__________________
-- bloomberg.
|
|
|
04-18-2003, 02:00 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
As abc123 says, it depends on how your input file is formed.
The way your program looks, is in a way, where you store the info for each person on seperate lines.
Since you use a static struct in your people defines, then you could have made the input file as beeing a binary file, with a leading double to tell you how many there were stored, so you would start by reading the number of people stored, then allocate the needed number of people in your input array, and just read each people struct binary.
To clarify, the file structure would be binary and formed like:
[double to indicate number of people stored][first person][secon person] .....
The only issues this would give, is you have to take into consideration how to handle big vs. small endian, which can be solved with running every integer through ntohl() or htonl().
A binary file would also make it easier to search through it.. Just
fseek(file_pointer, SEEK_CUR, how_many_people*sizeof(people_t));
Altho should there be an illformed storage within the file, every action performed on it, would just fsck the file up even more.
This could be solved with a simple crc checking algorithm.. But thats left as an exersize for the user.
|
|
|
04-18-2003, 02:19 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Apr 2003
Posts: 2
|
we were required to use formatted i/o in the first program (so binary is out). what i was trying to do with the 25 was limit the number of people whose information could be taken, since we weren't taught how to dynamically size a struct. is there a better way to do that? there's no requirement to limit the number of people, so i'm sure there are other possibilities. (i apologize if this is rambly or incoherent, i haven't been to sleep yet) any other suggestions?
(thank you all so much!)
|
|
|
04-18-2003, 02:44 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
hmm... if it's such a problem, then I would store every person on one line each, seperated with a <tab>, then when reading in from the file, you could do it like:
Code:
/*
Some code here which I wont reveal
maybe a main and some struct definition
*/
int people_counter = 0;
while(!feof(file_pointer))
{
/*
yea yea, I know fscanf() is insecure when it comes to
buffer overflow, but it was the first thing that came to me
*/
fscanf(file_pointer, "%s\t%s\t%s\t%d\t%d\t%s\t%d\n",
&x[people_counter].name, &x[people_counter].ssn,
&x[people_counter].city, &x[people_counter].age,
&x[people_counter].weight, &x[people_counter].eyes,
&x[people_counter].income);
people_counter++;
}
/*
here you can sum up the age and such,
using people_counter as the number to devide with
*/
bare in mind, none of the above has been tested, I wont say it's the correct thing to do, just a way to get your thoughts going, in what ways this might be solved.
|
|
|
06-05-2003, 11:02 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Feb 2003
Posts: 41
|
My noobishness is to a far higher caliber than you could ever hope to achieve. I dont even know what C++ is? Can anyone help me?
|
|
|
06-06-2003, 05:42 AM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
The C++ programming language is a further development of the orriginal C
C++ introduces OO with classes, garbage colector, STL library, overloading etc.
Theres much more... But I dont have the time to find all the varius links, so this will have to do for now.
|
|
|
06-13-2003, 08:52 AM
|
#8 (permalink)
|
|
Registered User
Join Date: Feb 2003
Posts: 41
|
Riiiiight. Next question what is C and all that other stuff you mentioned?
|
|
|
06-23-2003, 10:27 PM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Quote:
Orriginal post from Phaedrus
Next question what is C and all that other stuff you mentioned?
|
Did you take the time to actualy follow the links I included in the previus post ? Every thing I mention there are varius hyperlinks directing you to further information regarding that specific term.
|
|
|
06-24-2003, 07:47 AM
|
#10 (permalink)
|
|
Code Monkey
Join Date: Jan 2003
Posts: 57
|
Quote:
|
C++ introduces OO with classes, garbage colector, STL library, overloading etc.--redhead
|
C++ doesn't have a garbage collector you need to remove any dynamic memory you allocate yourself.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:18 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|