-1

如何通过读取 .txt 文件来创建数组?

我正在制作一个投标(拍卖)程序。我需要从 .txt 中读取整数并在数组中使用它们。我对如何在我的程序中进行此操作感到困惑。txt如下:

100 15 200 20 300 25 400 30 500 35

4

2 回答 2

2

It is definitely recommended to show one's own effort while asking the question. However, to give you a jump start, please find a couple of solutions as below.

One possible illustrative solution using a static array is as in Solution 1 below. The assumption in the solution below is that the number of elements is known and is less than 32. If wish to keep it truly dynamic, you will have to implement a solution using a linked list as in Solution 2 below.

Solution 1: Fixed Array based approach

int main()
{
    FILE    *finp;
    int     someArr[32];
    int     i, ctr = 0;

    finp = fopen("haha.txt", "r");
    if(NULL == finp)
    {
        printf("Unable to open file\n");
        exit(-1);
    }

    while((!feof(finp)) && (ctr < 32))
    {
        fscanf(finp, "%d ", &someArr[ctr++]);
    }

    for(i = 0; i < (ctr -1); i++)
    {
        printf("%d==>", someArr[i]);
    }
    printf("%d\n", someArr[i]);

    fclose(finp); //Close the file pointer

    return 0;
}

The expected output of this program is

100==>15==>200==>20==>300==>25==>400==>30==>500==>35

Solution 2: Linked List based solution

Further to my earlier comment, please find an alternative dynamic solution which doesn't require a prior knowledge on the number of elements as below.

typedef struct node {
int value;
struct node  *next;
}node;

void createList(FILE    *fInp, node **headBase)
{
    node    *currNode;
    node    *head = *headBase;
    node    *tail;

    while(!feof(fInp))
    {
        currNode = malloc(sizeof(struct node));
        fscanf(fInp, "%d ", &currNode->value);
        currNode->next = NULL;

        if(NULL == head)
        {
            head = currNode;
            tail = currNode;
        }
        else
        {
            tail->next = currNode;
            tail = currNode;
        }
    }

    //Store back the updated head pointer
    *headBase = head;
}

void  printList(node **headBase)
{
    node    *tmpNode = *headBase;

    while(tmpNode->next != NULL)
    {
        printf("%d-->", tmpNode->value);
        tmpNode = tmpNode->next;
    }
    printf("%d\n", tmpNode->value);
}

void deleteList(node **headBase)
{
    node    *head = *headBase;
    node    *tmp;

    while(NULL != head)
    {
        tmp = head; // Get a temp pointer
        head = head->next; // Move head pointer
        tmp->next = NULL; // break the link
        printf("<< Deleted Node: %d\n", tmp->value);
        free(tmp);
    }

    // Store the head pointer back which should be NULL
    *headBase = head;
}

int main()
{
    FILE    *finp;
    node    *head = NULL;

    finp = fopen("haha.txt", "r");
    if(NULL == finp)
    {
        printf("Unable to open file\n");
        exit(-1);
    }

    createList(finp, &head);

    printList(&head);

    deleteList(&head);

    fclose(finp);

    return 0;
}

The expected output of this program is

100-->15-->200-->20-->300-->25-->400-->30-->500-->35
<< Deleted Node: 100
<< Deleted Node: 15
<< Deleted Node: 200
<< Deleted Node: 20
<< Deleted Node: 300
<< Deleted Node: 25
<< Deleted Node: 400
<< Deleted Node: 30
<< Deleted Node: 500
<< Deleted Node: 35
于 2013-04-06T03:19:10.570 回答
1

假设您不知道在输入文件中可以找到多少个整数,您可以定义一个最大值。

你也必须包括stdio.hstdlib.h

#define MAX 255
int main() {
    FILE *file = fopen("input.txt", "r");
    int integers[MAX];
    int i = 0;
    if (file != NULL) {
        while (!feof(file) && i < MAX) {
            if (fscanf(file, "%d", &integers[i++]) != -1) {
                printf("%d ", integers[i-1]);
            }
        }
        fclose(file);
    } else {
        printf("Unable to open file");
        return EXIT_FAILURE;
    }

    return 0;
}
于 2013-04-06T03:36:25.797 回答