Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-18-2003, 12:08 AM   #1 (permalink)
p0etsou1
Registered User
 
Join Date: Apr 2003
Posts: 2
p0etsou1 is on a distinguished road
Send a message via AIM to p0etsou1
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.
p0etsou1 is offline   Reply With Quote
Old 04-18-2003, 01:18 AM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
... 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.
abc123 is offline   Reply With Quote
Old 04-18-2003, 02:00 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-18-2003, 02:19 AM   #4 (permalink)
p0etsou1
Registered User
 
Join Date: Apr 2003
Posts: 2
p0etsou1 is on a distinguished road
Send a message via AIM to p0etsou1
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!)
p0etsou1 is offline   Reply With Quote
Old 04-18-2003, 02:44 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-05-2003, 11:02 PM   #6 (permalink)
Phaedrus
Registered User
 
Phaedrus's Avatar
 
Join Date: Feb 2003
Posts: 41
Phaedrus is on a distinguished road
Send a message via ICQ to Phaedrus
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?
Phaedrus is offline   Reply With Quote
Old 06-06-2003, 05:42 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-13-2003, 08:52 AM   #8 (permalink)
Phaedrus
Registered User
 
Phaedrus's Avatar
 
Join Date: Feb 2003
Posts: 41
Phaedrus is on a distinguished road
Send a message via ICQ to Phaedrus
Riiiiight. Next question what is C and all that other stuff you mentioned?
Phaedrus is offline   Reply With Quote
Old 06-23-2003, 10:27 PM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-24-2003, 07:47 AM   #10 (permalink)
palin
Code Monkey
 
palin's Avatar
 
Join Date: Jan 2003
Posts: 57
palin is on a distinguished road
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.
palin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hello! Please forgive newbie question jimmyoctane PHP 4 09-13-2004 06:33 AM
Would you mind if there were small flash elements at code newbie? sde Lounge 10 05-29-2004 09:05 PM
Another newbie question. Odoggy5 PHP 26 09-03-2003 02:19 PM
DB Design Question Part II sde Program Design and Methods 3 05-10-2003 12:24 PM
Newbie Site ? DesertWolf Java 2 12-05-2002 12:38 PM


All times are GMT -8. The time now is 04:18 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting