Fred Flintstone 38 Male
Barney Rubble 36 Male
Wilma Flintstone 37 Female
Betty Rubble 36 Female
Pebbles Flintstone 4 Female
Bam-Bam Rubble 3 Male
Dino Flintstone 2 Male
IMPORTANT: There are 3 tabs (not spaces) before the numbers and 2 tabs after the numbers. I'm getting no errors or warnings, but my output box includes numbers that I do not want in there. I'm confused as to how to deal with the tabs. In the while loop, after the first fgets statement, the second argument is 19. And so, some of the strings that are read include the numbers from the text file as well. I've tried implementing the isdigit function to compensate for the tabs, but that doesn't affect the output of the numbers or the last string, at least due to the way I coded it. My best guess is that my main concern is reading the first string in the text file. Sorry if I wasn't really. This is my first time on here.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
typedef struct
{
char* name;
int* age;
char* gender;
}CHARACTER;
CHARACTER* pChar;
int charIndex;
char temp[40];
FILE* pData;
if ((pData = fopen("lab4data.txt", "r"))==NULL)
{
printf("Error opening file.\n");
return 100;
}
pChar = (CHARACTER*) calloc(7, sizeof(CHARACTER));
charIndex = 0;
while (charIndex<7)
{
fgets(temp, 19, pData);
pChar[charIndex].name = (char*)calloc(strlen(temp) + 1, sizeof(char));
strcpy(pChar[charIndex].name, temp);
pChar[charIndex].age = (int*)malloc(sizeof(int));
fscanf(pData, "%d", pChar[charIndex].age);
fgets(temp, 10, pData);
pChar[charIndex].gender = (char*)calloc(strlen(temp) + 1, sizeof(char));
strcpy(pChar[charIndex].gender, temp);
charIndex++;
}
for (charIndex=0; charIndex<7; charIndex++)
{
printf("%s %d%s", pChar[charIndex].name, *pChar[charIndex].age, pChar[charIndex].gender);
}
if (fclose(pData) == EOF )
{
printf("Error closing lab3data.txt\n");
return 200;
}
return 0;
}

New Topic/Question
Reply



MultiQuote




|