Hi...
I'm trying to figure out how to read in a file for a database program I'm making
This is what I have:
// ********************************************************************************
// FUNCTION: LOAD FILE
//
// Loads a text file into the database. Does not take any input, only uses the
// string that is given to it.
// *****************************************************************************
int loadFile ( char *fileName )
{
char choice, trash[5];
char readInBuffer[sizeof personType], *strTemp;
char *pToken; // points to where comma is found
bool temp = false;
FILE *pFile; // file pointer
// gets input (overwrite or append)
do
{
if (strlen(message)) printf("%s\n", message);
strcpy(message, ""
printf("Would you like to (a)ppend or (o)verwrite your currently loaded database? "
scanf("%c", &choice);
gets(trash);
if (choice != 'o' && choice != 'a')
strcpy(message, "Please enter a valid choice."
} while (strlen(message));
// clear database if they chose to
if (choice == 'o')
{
purgeDatabase();
temp = true;
}
// open the file
pFile = fopen(fileName, "r"
if (pFile == NULL) // checks to see if it actually opened
return 1;
// keep adding records until the end of the file
while (!feof(pFile))
{
// add one to record count
recCount++;
// previous record is now the last record, because we create a new one
(temp) ?
pPrevious = NULL : pPrevious = pLastRec;
// allocate a persontype struct for the current one
pLastRec = (struct personType *) malloc(sizeof (struct personType) );
if (pLastRec == NULL)
{
printf("Memory allocation failed, please free up more memory.\n"
system("pause"
return 1;
}
// no next, so null pointer
pLastRec->next = NULL;
// the previous->next one is this new record
if (pPrevious != NULL)
pPrevious->next = pLastRec;
// if pFirst is the previous record, then point the first record to this new one (only happens once)
if (temp)
{
pFirst->next = pLastRec;
temp = false;
}
fgets(readInBuffer, sizeof readInBuffer, pFile);
// use strtok to find the comma and parse the before it
pToken = strtok( readInBuffer, "," );
sscanf( strTemp, "%s", &pLastRec->lastName );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%s", &pLastRec->firstName );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%d", &pLastRec->age );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%c", &pLastRec->sex );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%c", &pLastRec->hairColor );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%d", &pLastRec->height );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%d", &pLastRec->weight );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%s", pLastRec->car, pToken );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%c", &pLastRec->carColor );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%d", &pLastRec->income );
pLastRec->number = recCount;
}
fclose(pFile);
pCurrent = pLastRec;
return 0;
}
the file is formatted like this:
argo,adam,19,m,y,74,220,explorer,g,100
it reads into a structure:
struct personType
{
char lastName[MAXSTRLEN]; // person's last name
char firstName[MAXSTRLEN]; // person's first name
int age; // person's age
char sex; // person's sex (m f)
char hairColor; // person's hair color (k y r b w g o)
int height; // person's height
int weight; // person's weight
char car[20]; // person's car
char carColor; // person's car color (k y r b w g t a o)
int income; // person's annual income, dollar
int number; // record number
personType *next; // pointer to next record
} *pFirst, *pCurrent, *pPrevious, *pLastRec;
it crashes every time... please help!
I'm trying to figure out how to read in a file for a database program I'm making
This is what I have:
// ********************************************************************************
// FUNCTION: LOAD FILE
//
// Loads a text file into the database. Does not take any input, only uses the
// string that is given to it.
// *****************************************************************************
int loadFile ( char *fileName )
{
char choice, trash[5];
char readInBuffer[sizeof personType], *strTemp;
char *pToken; // points to where comma is found
bool temp = false;
FILE *pFile; // file pointer
// gets input (overwrite or append)
do
{
if (strlen(message)) printf("%s\n", message);
strcpy(message, ""
printf("Would you like to (a)ppend or (o)verwrite your currently loaded database? "
scanf("%c", &choice);
gets(trash);
if (choice != 'o' && choice != 'a')
strcpy(message, "Please enter a valid choice."
} while (strlen(message));
// clear database if they chose to
if (choice == 'o')
{
purgeDatabase();
temp = true;
}
// open the file
pFile = fopen(fileName, "r"
if (pFile == NULL) // checks to see if it actually opened
return 1;
// keep adding records until the end of the file
while (!feof(pFile))
{
// add one to record count
recCount++;
// previous record is now the last record, because we create a new one
(temp) ?
pPrevious = NULL : pPrevious = pLastRec;
// allocate a persontype struct for the current one
pLastRec = (struct personType *) malloc(sizeof (struct personType) );
if (pLastRec == NULL)
{
printf("Memory allocation failed, please free up more memory.\n"
system("pause"
return 1;
}
// no next, so null pointer
pLastRec->next = NULL;
// the previous->next one is this new record
if (pPrevious != NULL)
pPrevious->next = pLastRec;
// if pFirst is the previous record, then point the first record to this new one (only happens once)
if (temp)
{
pFirst->next = pLastRec;
temp = false;
}
fgets(readInBuffer, sizeof readInBuffer, pFile);
// use strtok to find the comma and parse the before it
pToken = strtok( readInBuffer, "," );
sscanf( strTemp, "%s", &pLastRec->lastName );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%s", &pLastRec->firstName );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%d", &pLastRec->age );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%c", &pLastRec->sex );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%c", &pLastRec->hairColor );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%d", &pLastRec->height );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%d", &pLastRec->weight );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%s", pLastRec->car, pToken );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%c", &pLastRec->carColor );
pToken = strtok( NULL, "," );
strTemp = pToken;
sscanf( strTemp, "%d", &pLastRec->income );
pLastRec->number = recCount;
}
fclose(pFile);
pCurrent = pLastRec;
return 0;
}
the file is formatted like this:
argo,adam,19,m,y,74,220,explorer,g,100
it reads into a structure:
struct personType
{
char lastName[MAXSTRLEN]; // person's last name
char firstName[MAXSTRLEN]; // person's first name
int age; // person's age
char sex; // person's sex (m f)
char hairColor; // person's hair color (k y r b w g o)
int height; // person's height
int weight; // person's weight
char car[20]; // person's car
char carColor; // person's car color (k y r b w g t a o)
int income; // person's annual income, dollar
int number; // record number
personType *next; // pointer to next record
} *pFirst, *pCurrent, *pPrevious, *pLastRec;
it crashes every time... please help!