First things first, yes this is homework, yes I've been trying to figure it out on my own, yes I read similar questions on SO but haven't found the help I need.
I have a .txt file that I am reading into a struct and I am really struggling to understand the formatting for sscanf. I'm at a point now where I'm just trying any and everything, so I worry that if I do get it right it will be because I got lucky and not because I actually understand what I'm doing, which is where, hopefully, you fine folks could help.
Here is sample data from the .txt
4, Ben Appleseed, 1587 Apple Street, Salt Lake City, UT, 80514
2, Terri Lynn Smith, 1234 Slate Street, Cincinnati, OH, 45242
Note: Each 'field' is seperated by a space, comma, or tab. Some of the entries don't have a middle name, other wise each entry follow the same pattern. (If anyone has advice on how to handle the lines w/o all 8 fields I'm open to help)
This is my struct:
typedef struct
{
long lngRecordID;
char strFirstName[50];
char strMiddleName[50];
char strLastName[50];
char strStreet[100];
char strCity[50];
char strState[50];
char strZipCode[50];
} udtAddressType;
This is my routine to fill the structure
void AddAddressToArray(char strBuffer[], udtAddressType audtAddressList[])
{
int intIndex = 0;
for (intIndex = 0; intIndex < strBuffer[intIndex]; intIndex += 1)
{
if(sscanf(strBuffer, "%d, %s %s %s %s %s %s %s ",
&audtAddressList[intIndex].lngRecordID,
&audtAddressList[intIndex].strFirstName,
&audtAddressList[intIndex].strMiddleName,
&audtAddressList[intIndex].strLastName,
&audtAddressList[intIndex].strStreet,
&audtAddressList[intIndex].strCity,
&audtAddressList[intIndex].strState,
&audtAddressList[intIndex].strZipCode) != 8)
{
break;
}
}
}
That gives me an output of:
Address #50 -------------------------
Address ID: 4
First Name: Ben
Middle Name: Appleseed,
Last Name: 1587
Street Address: Apple
City: Street,
State: Salt
Zip Code: Lake
And that's not right.
I don't understand how to specify that I want the three address fields to be on one line. And a lot of what I've been reading is just confusing me further.
Function to load the file into the array:
void PopulateAddressList( udtAddressType audtAddressList[])
{
// Declare a file pointer
FILE* pfilInput = 0;
int intResultFlag = 0;
char strBuffer[50] = "";
char chrLetter = 0;
int intIndex = 0;
// Try to open the file for reading
intResultFlag = OpenInputFile("c:\\temp\\Addresses1.txt", &pfilInput);
// Was the file opened?
if (intResultFlag == 1)
{
// Yes, read in records until end of file( EOF )
while (feof(pfilInput) == 0)
{
// Read next line from file
fgets(strBuffer, sizeof(strBuffer), pfilInput);
AddAddressToArray(strBuffer, &audtAddressList[intIndex]);
intIndex += 1;
}
// Clean up
fclose(pfilInput);
}
}
Any help is much appreciated!