如何创建结构体数组 [英] how to create array of struct
问题描述
我想实现一个搜索表, 这是数据:
I want to implement a searching table and here's the data:
20130610 Diamond CoinMate 11.7246 15.7762 2897
20130412 Diamond Bithumb 0.209 0.2293 6128
20130610 OKCash Bithumb 0.183 0.2345 2096
20130412 Ethereum Chbtc 331.7282 401.486 136786
20170610 OKCash Tidex 0.0459 0.0519 66
...
和我的代码
typedef struct data{
int *date;
string currency[100];
string exchange[100];
double *low;
double *high;
int *daily_cap;
} Data;
int main()
{
FILE *fp = fopen("test_data.txt", "r");
Data tmp[50];
int i = 0;
while (!feof(fp)){
fscanf(fp, "%d%s%s%f%f%7d", &tmp[i].date, tmp[i].currency, tmp[i].exchange, &tmp[i].low, &tmp[i].high, &tmp[i].daily_cap);
i++;
}
fclose(fp);
}
但是第一个问题是我无法创建一个大型数组来存储我的结构体
but the first problem is that I can't create a large array to store my struct like
Data tmp[1000000]
甚至我只尝试50个元素,完成main()时程序都会崩溃. 谁能告诉我如何解决它或给我一个更好的方法,谢谢.
and even I try just 50 elements , the program break down when finish main(). can anyone tell how to fix it or give me a better method, thanks.
推荐答案
您不能将值扫描到未分配的空间,换句话说,您需要为struct
中的所有那些指针留出空间,切换到
You can not scan a value to an unallocated space, in other words, you need room for all those pointers in the struct
, switch to
typedef struct data{
int date;
string currency[100];
string exchange[100];
double low;
double high;
int daily_cap;
} Data;
或在使用它们之前使用malloc
为这些指针分配空间.
Or use malloc
to assign space to those pointers before using them.
while (!feof(fp)){
tmp[i].date = malloc(sizeof(int));
...
但是在这种情况下,您无需将此类成员的地址传递给fscanf
,因为它们已经是指针:
But in this case, you don't need to pass the address of such members to fscanf
since they are already pointers:
fscanf(fp, "%d%s%s%f%f%7d", &tmp[i].date, ..
应该是
fscanf(fp, "%d%s%s%lf%lf%7d", tmp[i].date, ...
请注意,double
希望使用%lf
而不是%f
Notice that double
wants %lf
instead of %f
这也很令人困惑:
typedef struct data{
int *date;
string currency[100];
...
string
是char
的typedef
吗?我认为您的意思是string currency;
,因为string
通常是char *
的别名,在这种情况下,您也需要为该成员提供空间:currency = malloc(100);
Is string
a typedef
of char
? I think you mean string currency;
since string
is usually an alias of char *
, in this case you need room for this member too: currency = malloc(100);
最后,看看为什么是"while(!feof(file ))"总是错吗?
简短的摘要中有太多错误,建议您阅读一本不错的C书.
There are too many errors in a short snippet, I suggest you to read a good C book.
使用动态内存更正了您的代码,该代码使您可以为大量数据保留空间(请参阅@LuisColorado的其他答案),并使用fgets
和sscanf
而不是fscanf
:
Your code corrected using dynamic memory that allows you to reserve space for a big amount of data (see the other answer of @LuisColorado) and using fgets
and sscanf
instead of fscanf
:
#include <stdio.h>
#include <stdlib.h>
typedef struct data{
int date;
char currency[100];
char exchange[100];
double low;
double high;
int daily_cap;
} Data;
int main(void)
{
FILE *fp = fopen("test_data.txt", "r");
/* Always check the result of fopen */
if (fp == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
Data *tmp;
tmp = malloc(sizeof(*tmp) * 50);
if (tmp == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
char buf[512];
int i = 0;
/* Check that you don't read more than 50 lines */
while ((i < 50) && (fgets(buf, sizeof buf, fp))) {
sscanf(buf, "%d%99s%99s%lf%lf%7d", &tmp[i].date, tmp[i].currency, tmp[i].exchange, &tmp[i].low, &tmp[i].high, &tmp[i].daily_cap);
i++;
}
fclose(fp);
/* Always clean what you use */
free(tmp);
return 0;
}
这篇关于如何创建结构体数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!