ah okay, i'll rethink it. It's supposed to read data from a file(text file) and put each line of that text file as a node on the linked list.
------------------Edit--------
now the readData part looks like this:
Code:
void readData( FILE* input )
{
Node *p; /* data from one input record */
int j;
int id, birthYear, height, weight, salary;
char firstName[nameSize], lastName[nameSize];
int flag; /* count of successful conversions in scanf */
/* Read in the File, copying data into the array */
while ( fgets( buffer, bufferSize, input) )
{ j++;
/* Read in the data */
flag = sscanf( buffer, "%d %d %d %d %d %s %s",
id, birthYear, height, weight, salary,
lastName, firstName );
if ( flag != 7 )
{
fprintf( stderr, "Data not formatted correctly: Record %d\n", j );
fclose( input );
exit( EXIT_FAILURE );
}
insertFirst(&p , id, birthYear, height, weight, salary, firstName, lastName);
p = p->link;
}
}
I have a feeling something is wrong with it and/or that making those extra variables is unnecessary somehow. The entire program looks like this now
Code:
/* Mini-data base. Somewhat more than was asked for with program 10 */
#include <stdio.h>
#include <stdlib.h>
#define nameSize 50
/* Data for one person */
struct NODE
{
struct NODE *link;
int id, birthYear, height, weight, salary;
char firstName[nameSize], lastName[nameSize];
};
typedef struct NODE Node;
Node *insertFirst( Node **hptr, int id, int birthYear, int height, int weight, int salary, char firstName [nameSize], char lastName[nameSize])
{
Node *node = (Node *)malloc( sizeof( Node ) );
node->id = id;
node->link = *hptr;
node->birthYear = birthYear;
node->height = height;
node->weight = weight;
node->salary = salary;
strcopy(node->firstName, firstName);
strcopy(node->lastName, lastName);
*hptr = node;
return node;
}
#define bufferSize 132
char buffer[bufferSize]; /* input buffer */
/* Print nicely formatted data for one person */
void printRecord( Node *p )
{
printf("id: %03d birth: %04d height: %02d weight: %03d salary: %06d name: %s %s\n",
p->id, p->birthYear,p->height,p->weight,
p->salary,p->firstName,p->lastName);
}
/* Ask the user for the id of a person */
int getID()
{
int id;
printf("id? (enter -1 to quit):");
gets( buffer );
while ( sscanf( buffer, " %d", &id ) != 1 )
{
printf("re-enter id: ");
gets( buffer );
}
return id;
}
void readData( FILE* input )
{
Node *p; /* data from one input record */
int j;
int id, birthYear, height, weight, salary;
char firstName[nameSize], lastName[nameSize];
int flag; /* count of successful conversions in scanf */
/* Read in the File, copying data into the array */
while ( fgets( buffer, bufferSize, input) )
{ j++;
/* Read in the data */
flag = sscanf( buffer, "%d %d %d %d %d %s %s",
id, birthYear, height, weight, salary,
lastName, firstName );
if ( flag != 7 )
{
fprintf( stderr, "Data not formatted correctly: Record %d\n", j );
fclose( input );
exit( EXIT_FAILURE );
}
insertFirst(&p , id, birthYear, height, weight, salary, firstName, lastName);
p = p->link;
}
}
freeList()
{
}
int main()
{
FILE *input;
Node *head = NULL;
int j=0;
char fileName[bufferSize];
/* Ask user for input file; open file */
printf("Name of input file: ");
gets( buffer );
sscanf( buffer, "%s", fileName );
/* Open input file for reading */
if ( (input=fopen( fileName, "r"))==NULL )
{
perror("input file did not open ");
exit( EXIT_FAILURE );
}
/* Read in the data */
readData( input );
/* Process Queries until user enters -1 */
int id = 0 ;
id = getID();
while ( id != -1 )
{
/* Linear search */
while(head->link != NULL)
{
if ( head->id == id )
printRecord( head );
else
printf("id %3d not found\n", id );
head = head->link;
}
id = getID();
}
/* Write out the data in order of id */
printf("\n\nAll Records:\n");
while(head->link != NULL)
if ( head->id != 0 )
printRecord( head );
fclose( input );
system("pause");
}
it says linker error undefined reference to strcopy. I thought that part was settled... also ld returned 1 exit status.