怎么样把txt文件中的数据添加到数组中?
程序代码:
蓝色部分是应该接下来要做的,可是不知道怎么把txt的数据添加到数组里,大概知道应该先添加到数组里然后在分开然后print。 有大神帮忙指导一下或者给个模板例子也好, // -------------------------------------------------------------------------------- // Includes // -------------------------------------------------------------------------------- #define _CRT_SECURE_NO_DEPRECATE #include<stdio.h> #include<stdlib.h> // -------------------------------------------------------------------------------- // Constants // -------------------------------------------------------------------------------- const int intARRAY_SIZE = 100; // -------------------------------------------------------------------------------- // User Defined Types (UDTs) // -------------------------------------------------------------------------------- typedef struct { long lngRecordID; char strFullName[50]; char strFirstName[50]; char strMiddleName[50]; char strLastName[50]; char strStreet[100]; char strCity[50]; char strState[50]; char strZipCode[50]; } udtAddressType; // -------------------------------------------------------------------------------- // Prototypes // -------------------------------------------------------------------------------- void InitializeAddressList(udtAddressType audtAddressList[]); void InitializeAddress(udtAddressType* pudtAddress); void StringCopy(char strDestination[], char strSource[]); void PopulateAddressList(udtAddressType audtAddressList[]); int OpenInputFile(char strFileName[], FILE** ppfilInput); void AddAddressToArray(char strBuffer, udtAddressType audtAddressList[]); void PrintAddressList(udtAddressType audtAddressList[]); void PrintAddress(int intIndex, udtAddressType udtAddress); // String Functions // -------------------------------------------------------------------------------- // Name: main // Abstract: This is where the program starts. // -------------------------------------------------------------------------------- void main() { udtAddressType audtAddressList[100]; InitializeAddressList(audtAddressList); PopulateAddressList(audtAddressList); PrintAddressList(audtAddressList); system("pause"); } // -------------------------------------------------------------------------------- // Name: InitializeAddressList // Abstract: Intialize all the Address in the list // -------------------------------------------------------------------------------- void InitializeAddressList(udtAddressType audtAddressList[]) { int intIndex = 0; for (intIndex = 0; intIndex < intARRAY_SIZE; intIndex += 1) { // Pass a single array element by pointer InitializeAddress(&audtAddressList[intIndex]); } } // -------------------------------------------------------------------------------- // Name: InitializeAddress // Abstract: Set all the values to 0 or empty strings // -------------------------------------------------------------------------------- void InitializeAddress(udtAddressType* pudtAddress) { pudtAddress->lngRecordID = 0; StringCopy(pudtAddress->strFirstName, ""); StringCopy(pudtAddress->strMiddleName, ""); StringCopy(pudtAddress->strLastName, ""); StringCopy(pudtAddress->strStreet, ""); StringCopy(pudtAddress->strCity, ""); StringCopy(pudtAddress->strState, ""); StringCopy(pudtAddress->strZipCode, ""); } // -------------------------------------------------------------------------------- // Name: StringCopy // Abstract: Copy the source to the destination // -------------------------------------------------------------------------------- void StringCopy(char strDestination[], char strSource[]) { int intIndex = 0; // Copy each character while (strSource[intIndex] != 0) { strDestination[intIndex] = strSource[intIndex]; intIndex += 1; } // Terminate strDestination[intIndex] = 0; } // -------------------------------------------------------------------------------- // Name: PopulateAddressList // Abstract: Load the addresses from a 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 (notice you have to double up the backslashes) 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); // Read one character chrLetter = fgetc( pfilInput ); // 1. save reading one character at time until the very end // 2. save breaking apart the full name into first, middle and last until // 2nd last thing from the end AddAddressToArray(strBuffer, &audtAddressList[intIndex]); // Print out line to screen printf("%s\n", strBuffer); } // Clean up fclose(pfilInput); } } // -------------------------------------------------------------------------------- // Name: OpenInputFile // Abstract: Open the file for reading. Return true if successful. // -------------------------------------------------------------------------------- int OpenInputFile(char strFileName[], FILE** ppfilInput) { int intResultFlag = 0; // Open the file for reading *ppfilInput = fopen(strFileName, "rb"); // Success? if (*ppfilInput != 0) { // Yes intResultFlag = 1; } else { // No printf("Error opening %s for reading!!!\n", strFileName); } return intResultFlag; } // -------------------------------------------------------------------------------- // Name: AddAddressToArray // Abstract: Add Address To Array // -------------------------------------------------------------------------------- void AddAddressToArray(char strBuffer, udtAddressType audtAddressList[]) { int intIndex = 0; for (intIndex = 0; intIndex < intARRAY_SIZE; intIndex += 1) { PrintAddressList(intIndex, audtAddressList[intIndex]); } } // -------------------------------------------------------------------------------- // Name: PrintAddressList // Abstract: Print all the Address // -------------------------------------------------------------------------------- void PrintAddressList(udtAddressType audtAddressList[]) { int intIndex = 0; for (intIndex = 0; intIndex < intARRAY_SIZE; intIndex += 1) { // Pass a single array element PrintAddress(intIndex, audtAddressList[intIndex]); } } // -------------------------------------------------------------------------------- // Name: PrintAddress // Abstract: Print all the structure field values // -------------------------------------------------------------------------------- void PrintAddress(int intIndex, udtAddressType udtAddress) { printf("\n"); printf("Address #%2d --------------------------------\n", intIndex + 1); printf("\tRecord ID : %ld\n", udtAddress.lngRecordID); printf("\tFitst Name : %s\n", udtAddress.strFirstName); printf("\tMiddle Name : %s\n", udtAddress.strMiddleName); printf("\tLast Name : %s\n", udtAddress.strLastName); printf("\tStreet Address : %s\n", udtAddress.strStreet); printf("\tCity : %s\n", udtAddress.strCity); printf("\tState : %s\n", udtAddress.strState); printf("\tZipcode : %s\n", udtAddress.strZipCode); } // -------------------------------------------------------------------------------- // Name: String Functions // Abstract: Print all the structure field values // --------------------------------------------------------------------------------
[ 本帖最后由 Yuny777 于 2015-7-28 05:30 编辑 ]