| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 332 人关注过本帖
标题:怎么样把txt文件中的数据添加到数组中?
只看楼主 加入收藏
Yuny777
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-7-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
怎么样把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 编辑 ]
2015-07-28 05:23
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
谁知道你想干嘛呢

DO IT YOURSELF !
2015-07-28 08:11
快速回复:怎么样把txt文件中的数据添加到数组中?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027169 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved