Im trying to read from ONE file a list of a last name, first name, street address, and phone number (xxx-xxx-xxxx), instead of having to do it the way it is below. Any suggestions on how to read it into the program, and then print it, would be a great help. Thanks.
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char fname[25];
char lname[25];
char address1[25];
char address2[25];
char phone[25];
char type[25];
struct node *next;
};
void search(struct node *);
//void show_list(struct node *);
void lastNamesearch(char *);
int main()
{
////////HERE IS WHERE I NEED HELP, THANKS///////////////////////////
FILE *firstName,*lastName,*address1,*address2,*phone,*t ype;
struct node *pstart,*pprev,*pnow, *result;
char lastname[25];
char cont = 'Y';
firstName=fopen("firstName.txt","w");
lastName=fopen("lastName.txt","w");
address1=fopen("address1.txt","w");
address2=fopen("address2.txt","w");
phone=fopen("phone.txt","w");
type=fopen("type.txt","w");
if(firstName==NULL)
{printf("Error opening file");
exit(0);}
if(lastName==NULL)
{printf("Error opening file");
exit(0);}
if(address1==NULL)
{printf("Error opening file");
exit(0);}
if(address2==NULL)
{printf("Error opening file");
exit(0);}
if(phone==NULL)
{printf("Error opening file");
exit(0);}
fprintf(lastName," %s\n %s\n %s\n %s\n %s\n","Austin","Miller","Harris","Georgh","Gray");
fprintf(firstName," %s\n %s\n %s\n %s\n %s\n","David","James","Troy","Noel","Brain");
fprintf(address1," %s\n %s\n %s\n %s\n %s\n","417 N.Grant Street","213 S.Grant Street",
"3123 college Avenue","2756 Third street",
"2345 Tenth Street");
fprintf(address2," %s\n %s\n %s\n %s\n %s\n","Bloomington, IN - 47405","Bloomington, IN - 47408",
"Bloomington, IN - 47409","Bloomington, IN - 47403",
"Bloomington, IN - 47405");
fprintf(phone," %s\n %s\n %s\n %s\n %s\n","812-3359277","812-3359247","812-3355277",
"812-3354277","812-3355275");
fprintf(type," %s\n %s\n %s\n %s\n %s\n","Friend","Family","Business",
"Friend","Family");
fclose(lastName);
fclose(firstName);
fclose(address1);
fclose(address2);
fclose(phone);
fclose(type);
firstName=fopen("firstName.txt","r");
lastName=fopen("lastName.txt","r");
address1=fopen("address1.txt","r");
address2=fopen("address2.txt","r");
phone=fopen("phone.txt","r");
type=fopen("type.txt","r");
pstart=pprev=NULL; /*the list is empty*/
while (!feof(lastName))
{ pnow = (struct node *)malloc(sizeof(struct node));
if (pnow == NULL)
printf("Unable to allocate memory");
if (pstart == NULL)
pstart = pnow;
else
pprev->next = pnow;
fgets(pnow->lname,25,lastName);
fgets(pnow->fname,25,firstName);
fgets(pnow->address1,25,address1);
fgets(pnow->address2,25,address2);
fgets(pnow->phone,25,phone);
fgets(pnow->type,25,type);
pnow->next = NULL;
pprev = pnow;
////////////THAT IS THE END, FOR NOW//////////////////////////////
}
search(pstart);
//lastNamesearch(lastname);
//printf("%s",lastname);
//show_list(pstart);
//result = (struct node *)malloc(sizeof(struct node));
//result = search(pstart,lastname);
//printf("%s",result->fname);
return 0;
}
void search(struct node *ps)
{ struct node *p;
char searchLName[25];
p = ps;
printf(" Please enter the Last name to be searched ");
gets(searchLName);
while (p!= NULL)
{ if ( strcmp( searchLName, p->lname )==0)
printf("%s",p->fname);
//return p;
p = p -> next; }
return ;//NULL;
}
/*void lastNamesearch(char searchLName[25])
{
printf(" Please enter the Last name to be searched ");
gets(searchLName);
return;
}
*/
void firstNamesearch(char searchFName[25])
{
printf(" Please enter the First name to be searched ");
gets(searchFName);
return;
}
/*
void show_list(struct node *ps)
{ printf("Here is the list\n\n");
do {
printf("%s\n%s %s\n %s\n %s\n %s\n",ps->type,ps->lname,ps->fname,ps->address1,ps->address2,
ps->phone);
ps = ps->next;
}while (ps != NULL);
return;
}
*/