【问题标题】:return permanent pointer to array after saving delimited text file to it将分隔的文本文件保存到数组后返回指向数组的永久指针
【发布时间】:2015-05-24 16:52:26
【问题描述】:

我正在尝试存储一个如下所示的文本文件:

Headphones-750, TV-1500, Keyboard-1200, Tablet-80, Speakers-400, DVD-250, Streamer-550, Mouse-50; 

放入一个def类型的数组中:

    typedef struct item{
        char* name;
        int price;
    }item;

    typedef struct item{
        char* name;
        int product_code;
        int price;
    }item;



item *getProducts(){
    FILE *fp;
    fp = fopen("machinedata.txt", "r");

    if (fp == NULL)
    {
        printf("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    static item products[8];
    int row = 0;

    while (fscanf(fp,"%50[^-],%10[^,]",products[row].name,products[row].price) ){
        products[row].product_code = row;
        row++;
    }

    return products;
}

但它不会工作。我如何创建这个typedef item 数组并返回一个指向数组的指针,我将能够从其他函数特别是 main() 操作/打印该数组。

我正在用 C 编码,请寻找 C 指导

【问题讨论】:

  • 不要定义结构。结果是代码混乱、误解和编译器名称空间混乱。
  • 您没有为products[row].name 分配任何内存,因此将未初始化的指针传递给fscanf

标签: c arrays scanf


【解决方案1】:

item 有一个 typedef。
item products[8]; 在 main 中,可以传递给其他函数。
items 的 name 成员是一个指针。需要为其分配内存来存储文件中的字符串。
格式字符串" %50[^-]-%d%*c" 将跳过前导空格,扫描最多 50 个非“-”字符,扫描一个“-”,扫描一个整数,然后扫描并丢弃整数后面的字符。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct item{
    char* name;
    int product_code;
    int price;
}item;

int getProducts ( item prods[], int size);

int main ( ) {
    int read = 0;
    int each = 0;
    item products[8];

    read = getProducts ( products, sizeof ( products) / sizeof ( products[0]));
    for ( each = 0; each < read; each++) {
        printf ( "%s %d %d\n", products[each].name, products[each].price, products[each].product_code);
    }
    for ( each = 0; each < read; each++) {
        free ( products[each].name);//release memory
    }
    return 0;
}

int getProducts ( item prods[], int size) {
    char name[51] = {0};
    int row = 0;

    FILE *fp;
    fp = fopen("machinedata.txt", "r");

    if (fp == NULL)
    {
        printf("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    while ((fscanf(fp," %50[^-]-%d%*c",name,&prods[row].price))==2){
        prods[row].name = malloc ( strlen ( name) + 1);//allocate memory for name
        if ( prods[row].name == NULL) {
            printf ( "Malloc failed\n");
            break;
        }
        strcpy ( prods[row].name, name);//copy name into structure
        prods[row].product_code = row;
        row++;
        if ( row >= size) {
            break;
        }
    }

    return row;
}

【讨论】:

    【解决方案2】:

    代码有一些问题。

    1) it does not compile.
        it needs #include <stdio.h>
        and #include <stdlib.h>
    2) the struct 'item' is defined two different ways.
        suggest remove the typdef that does not have the product code field
    3) the second input conversion identifier in the format string
       in the fscanf() statement is expecting a char* parameter,
       but the parameter is int*.
    

    那么,它连编译都没有,怎么能说不工作呢?

    建议编译时启用所有警告,(对于 gcc '-Wall -Wextra -pedantic')修复警告,然后重新发布代码

    【讨论】:

      【解决方案3】:

      您已多次定义typedef struct item。编译器应该发出报告错误。

      在fscanf(...,products[row].price)中你应该传递int的地址,而不是它的值,所以写&amp;products[row].price)。

      建议测试读取的行数不超过 8 行,例如 if (row&lt;8 &amp;&amp; fscanf(....))。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 2014-12-30
        • 2021-06-25
        • 1970-01-01
        相关资源
        最近更新 更多