D
dtschoepe
Hi all,
Working on homework again...
I've got a weird problem, I've been banging my head against the wall
on what is causing it. I have a pointer to a typdef named Person. At
one point in the code the pointer changes from storing the memory
location of the Person and contains the value '1'.
I have commented out several pieces of code in order to try to
eliminate the cause but no success so far. I have also inserted in the
code comments where the problem or interesting areas are located. The
code consists of 4 files, I will list their contents here....
matchmaker.c
functions.c
functions.h
input.txt
What happens is that the program reads in the file "input.txt". I have
limited it to reading one set of data until I fix the seg fault issue
(commented out while loop). After it reads in the 1 set of data I have
a line that is supposed to print the first name of the "Person" using
a pointer points to firstname call in matchmaker.c (after the
commented while loop). I have commented the line right before where
the seg fault happens and where the pointer has somehow gotten the
value '1' assigned into it.
Hopefully I can learn something from someone here tonight.
#include "functions.h"
int main(int argc, char * argv[])
{
char *filename;
FILE *fin;
FILE *fout;
Person *memberptr;
Person member;
int error, count=0;;
filename = getFileName();
fin = openFile(filename, "r");
fout = openFile("HW3output.txt", "w");
printf("\nMemberPTR contents: %d\n", *memberptr);
printf("MemberPTR address: %x\n\n", &memberptr);
/* get member details and create nodes */
//while (!feof(fin))
//{
printf("count value: %d\n", count);
memberptr = createMember(fin);
printf("Name %s\n", memberptr->firstName);
// Here *memberptr has the value of a memory location
printf("MemberPTR contents: %x\n", *memberptr);
printf("MemberPTR address: %x\n", &memberptr);
//addMember(memberptr);
printf("Name %s\n", memberptr->firstName);
count++;
printf("member added, count value: %d\n", count);
//}
// Here is where *memberptr == 1
// Why would it change from a memory address to 1 here?
printf("out of while loop\n");
printf("MemberPTR contents: %x\n", *memberptr);
printf("MemberPTR address: %x\n", &memberptr);
// Here we get a seg fault because memberptr == 1 and not a valid
memory location
printf("Name %s\n", memberptr->firstName);
printMember(memberptr);
sortMembers();
matchMembers();
/* free memory */
deleteAll();
/* close files */
error = closeFile(fin);
error = closeFile(fout);
return EXIT_SUCCESS;
}
/* functions.c */
#include "functions.h"
Node *head = NULL;
Person *createMember(FILE *infile)
{
char fname[30];
char lname[30];
char sex[10];
char age[10];
char color[20];
char hobby[30];
char *colorS;
Person member;
Person *memberptr;
if (!feof(infile))
{
fgets(fname, 30, infile);
fgets(lname, 30, infile);
fgets(sex, 10, infile);
fgets(age, 10, infile);
fgets(color, 20, infile);
fgets(hobby, 30, infile);
}
else
return NULL;
/* read member attributes from file */
if (fname[strlen(fname)-1] == '\n')
fname[strlen(fname)-1] = '\0';
if (lname[strlen(lname)-1] == '\n')
lname[strlen(lname)-1] = '\0';
if (color[strlen(color)-1] == '\n')
color[strlen(color)-1] = '\0';
if (hobby[strlen(hobby)-1] == '\n')
hobby[strlen(hobby)-1] = '\0';
/* create memory allocation for member attributes */
member.firstName = calloc(strlen(fname)+1, 1);
member.lastName = malloc(sizeof(lname));
member.hobby = malloc(sizeof(hobby));
colorS = malloc(sizeof(color));
/* store member attributes */
strcpy(member.firstName, fname);
strcpy(member.lastName, lname);
sscanf(sex, "%c", &member.sex);
sscanf(age, "%d", &member.age);
strcpy(colorS, color);
member.favColor = readColor(colorS);
strcpy(member.hobby, hobby);
memberptr = &member;
printf("memberptr: %x\n\n", *memberptr);
return memberptr;
}
enum color readColor(char *theFavColor)
{
enum colors { COLORS };
enum colors theColor;
if ( strcmp(theFavColor, "red") == 0 )
theColor = red;
else if ( strcmp(theFavColor, "blue") == 0)
theColor = blue;
else if ( strcmp(theFavColor, "green") == 0)
theColor = green;
else if ( strcmp(theFavColor, "yellow") == 0)
theColor = yellow;
else if ( strcmp(theFavColor, "black") == 0)
theColor = black;
else if ( strcmp(theFavColor, "purple") == 0)
theColor = purple;
else if ( strcmp(theFavColor, "pink") == 0)
theColor = pink;
else
theColor = none;
return theColor;
}
void printMember(Person *memberptr)
{
char color[10];
switch(memberptr->favColor)
{
case red :
strcpy(color, "red");
break;
case blue :
strcpy(color, "blue");
break;
case green :
strcpy(color, "green");
break;
case yellow :
strcpy(color, "yellow");
break;
case black :
strcpy(color, "black");
break;
case purple :
strcpy(color, "purple");
break;
case pink :
strcpy(color, "pink");
break;
default:
strcpy(color, "N/A");
break;
}
printf("starting to print person\n");
printf("Name: %s %s\n", memberptr->firstName, memberptr->lastName);
printf("Sex: %c\n", memberptr->sex);
printf("Age: %d\n", memberptr->age);
printf("Favorite Color: %s\n", color);
printf("Hobby: %s\n\n", memberptr->hobby);
}
int addMember(Person *member) {
Node *cur = NULL;
cur = (Node *)malloc(sizeof(Node));
if(cur == NULL)
return 0;
cur -> client = member;
cur -> next = NULL;
if(head == NULL)
head = cur;
else
{
cur -> next = head;
head = cur;
}
return 1;
}
void deleteAll() {
}
void sortMembers() {
}
void matchMembers() {
}
/* ask user for filename and return char pointer */
char *getFileName()
{
char temp[100];
char *filename;
int len = 0;
printf("Enter a filename to open: ");
fgets(temp, sizeof(temp), stdin);
len = strlen(temp);
temp[len-1] = '\0';
len = strlen(temp);
filename = (char *)malloc(sizeof(char) * (len - 1));
strcpy(filename, temp);
return filename;
}
FILE *openFile(char *filename, char *mode)
{
FILE *fp;
while ((fp = fopen(filename, mode)) == NULL)
{
printf("The file '%s' was not found. Enter a file to open.\n",
filename);
printf("Or enter 'q' to quit: ");
filename = getFileName();
if (filename[0] == 'q' && strlen(filename) == 1)
break;
}
return fp;
}
int closeFile(FILE *file)
{
int error = 0;
if (file != NULL)
fclose(file);
else
error = 1;
return error;
}
/* functions.h */
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COLORS red, blue, green, yellow, black, purple, pink, none
enum color { COLORS };
typedef enum color Color;
typedef struct person
{
char *firstName;
char *lastName;
char *hobby;
Color favColor;
char sex;
int age;
} Person;
struct node
{
struct node *next;
Person *client;
};
typedef struct node Node;
/* Node functions */
int addMember(Person *member);
void deleteAll();
/* Person functions */
Person *createMember(FILE *infile);
enum color readColor(char *theFavColor);
void printPerson(Person *member);
void sortMembers();
void matchMembers();
/* I/O functions */
char *getFileName();
FILE *openFile(char *filename, char *mode);
int closeFile(FILE *file);
#endif
/* input.txt */
bubba
von bigbelly
m
44
purple
watching pokemon
sally sue
sullivan
f
40
purple
working out
faith
hill
f
32
green
writing songs
David
Working on homework again...
I've got a weird problem, I've been banging my head against the wall
on what is causing it. I have a pointer to a typdef named Person. At
one point in the code the pointer changes from storing the memory
location of the Person and contains the value '1'.
I have commented out several pieces of code in order to try to
eliminate the cause but no success so far. I have also inserted in the
code comments where the problem or interesting areas are located. The
code consists of 4 files, I will list their contents here....
matchmaker.c
functions.c
functions.h
input.txt
What happens is that the program reads in the file "input.txt". I have
limited it to reading one set of data until I fix the seg fault issue
(commented out while loop). After it reads in the 1 set of data I have
a line that is supposed to print the first name of the "Person" using
a pointer points to firstname call in matchmaker.c (after the
commented while loop). I have commented the line right before where
the seg fault happens and where the pointer has somehow gotten the
value '1' assigned into it.
Hopefully I can learn something from someone here tonight.
#include "functions.h"
int main(int argc, char * argv[])
{
char *filename;
FILE *fin;
FILE *fout;
Person *memberptr;
Person member;
int error, count=0;;
filename = getFileName();
fin = openFile(filename, "r");
fout = openFile("HW3output.txt", "w");
printf("\nMemberPTR contents: %d\n", *memberptr);
printf("MemberPTR address: %x\n\n", &memberptr);
/* get member details and create nodes */
//while (!feof(fin))
//{
printf("count value: %d\n", count);
memberptr = createMember(fin);
printf("Name %s\n", memberptr->firstName);
// Here *memberptr has the value of a memory location
printf("MemberPTR contents: %x\n", *memberptr);
printf("MemberPTR address: %x\n", &memberptr);
//addMember(memberptr);
printf("Name %s\n", memberptr->firstName);
count++;
printf("member added, count value: %d\n", count);
//}
// Here is where *memberptr == 1
// Why would it change from a memory address to 1 here?
printf("out of while loop\n");
printf("MemberPTR contents: %x\n", *memberptr);
printf("MemberPTR address: %x\n", &memberptr);
// Here we get a seg fault because memberptr == 1 and not a valid
memory location
printf("Name %s\n", memberptr->firstName);
printMember(memberptr);
sortMembers();
matchMembers();
/* free memory */
deleteAll();
/* close files */
error = closeFile(fin);
error = closeFile(fout);
return EXIT_SUCCESS;
}
/* functions.c */
#include "functions.h"
Node *head = NULL;
Person *createMember(FILE *infile)
{
char fname[30];
char lname[30];
char sex[10];
char age[10];
char color[20];
char hobby[30];
char *colorS;
Person member;
Person *memberptr;
if (!feof(infile))
{
fgets(fname, 30, infile);
fgets(lname, 30, infile);
fgets(sex, 10, infile);
fgets(age, 10, infile);
fgets(color, 20, infile);
fgets(hobby, 30, infile);
}
else
return NULL;
/* read member attributes from file */
if (fname[strlen(fname)-1] == '\n')
fname[strlen(fname)-1] = '\0';
if (lname[strlen(lname)-1] == '\n')
lname[strlen(lname)-1] = '\0';
if (color[strlen(color)-1] == '\n')
color[strlen(color)-1] = '\0';
if (hobby[strlen(hobby)-1] == '\n')
hobby[strlen(hobby)-1] = '\0';
/* create memory allocation for member attributes */
member.firstName = calloc(strlen(fname)+1, 1);
member.lastName = malloc(sizeof(lname));
member.hobby = malloc(sizeof(hobby));
colorS = malloc(sizeof(color));
/* store member attributes */
strcpy(member.firstName, fname);
strcpy(member.lastName, lname);
sscanf(sex, "%c", &member.sex);
sscanf(age, "%d", &member.age);
strcpy(colorS, color);
member.favColor = readColor(colorS);
strcpy(member.hobby, hobby);
memberptr = &member;
printf("memberptr: %x\n\n", *memberptr);
return memberptr;
}
enum color readColor(char *theFavColor)
{
enum colors { COLORS };
enum colors theColor;
if ( strcmp(theFavColor, "red") == 0 )
theColor = red;
else if ( strcmp(theFavColor, "blue") == 0)
theColor = blue;
else if ( strcmp(theFavColor, "green") == 0)
theColor = green;
else if ( strcmp(theFavColor, "yellow") == 0)
theColor = yellow;
else if ( strcmp(theFavColor, "black") == 0)
theColor = black;
else if ( strcmp(theFavColor, "purple") == 0)
theColor = purple;
else if ( strcmp(theFavColor, "pink") == 0)
theColor = pink;
else
theColor = none;
return theColor;
}
void printMember(Person *memberptr)
{
char color[10];
switch(memberptr->favColor)
{
case red :
strcpy(color, "red");
break;
case blue :
strcpy(color, "blue");
break;
case green :
strcpy(color, "green");
break;
case yellow :
strcpy(color, "yellow");
break;
case black :
strcpy(color, "black");
break;
case purple :
strcpy(color, "purple");
break;
case pink :
strcpy(color, "pink");
break;
default:
strcpy(color, "N/A");
break;
}
printf("starting to print person\n");
printf("Name: %s %s\n", memberptr->firstName, memberptr->lastName);
printf("Sex: %c\n", memberptr->sex);
printf("Age: %d\n", memberptr->age);
printf("Favorite Color: %s\n", color);
printf("Hobby: %s\n\n", memberptr->hobby);
}
int addMember(Person *member) {
Node *cur = NULL;
cur = (Node *)malloc(sizeof(Node));
if(cur == NULL)
return 0;
cur -> client = member;
cur -> next = NULL;
if(head == NULL)
head = cur;
else
{
cur -> next = head;
head = cur;
}
return 1;
}
void deleteAll() {
}
void sortMembers() {
}
void matchMembers() {
}
/* ask user for filename and return char pointer */
char *getFileName()
{
char temp[100];
char *filename;
int len = 0;
printf("Enter a filename to open: ");
fgets(temp, sizeof(temp), stdin);
len = strlen(temp);
temp[len-1] = '\0';
len = strlen(temp);
filename = (char *)malloc(sizeof(char) * (len - 1));
strcpy(filename, temp);
return filename;
}
FILE *openFile(char *filename, char *mode)
{
FILE *fp;
while ((fp = fopen(filename, mode)) == NULL)
{
printf("The file '%s' was not found. Enter a file to open.\n",
filename);
printf("Or enter 'q' to quit: ");
filename = getFileName();
if (filename[0] == 'q' && strlen(filename) == 1)
break;
}
return fp;
}
int closeFile(FILE *file)
{
int error = 0;
if (file != NULL)
fclose(file);
else
error = 1;
return error;
}
/* functions.h */
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COLORS red, blue, green, yellow, black, purple, pink, none
enum color { COLORS };
typedef enum color Color;
typedef struct person
{
char *firstName;
char *lastName;
char *hobby;
Color favColor;
char sex;
int age;
} Person;
struct node
{
struct node *next;
Person *client;
};
typedef struct node Node;
/* Node functions */
int addMember(Person *member);
void deleteAll();
/* Person functions */
Person *createMember(FILE *infile);
enum color readColor(char *theFavColor);
void printPerson(Person *member);
void sortMembers();
void matchMembers();
/* I/O functions */
char *getFileName();
FILE *openFile(char *filename, char *mode);
int closeFile(FILE *file);
#endif
/* input.txt */
bubba
von bigbelly
m
44
purple
watching pokemon
sally sue
sullivan
f
40
purple
working out
faith
hill
f
32
green
writing songs
David