return 保存分隔文本文件后指向数组的永久指针

return permanent pointer to array after saving delimited text file to it

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

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 数组和 return 一个指向数组的指针,我将能够 manipulate/print 从其他函数,特别是 main() 。

我正在用 C 编写代码并正在寻找 C 指南

代码有一些问题。

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')修复警告,然后重新post 代码

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

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

建议测试不读取超过8行,例如if (row<8 && fscanf(....)).

item 有一个 typedef。
item products[8]; 位于 main 中,可以将其传递给其他函数。
itemsname 成员是一个指针。需要为其分配内存以存储文件中的字符串。
格式字符串 " %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;
}