gpt4 book ai didi

c - 将 CSV 解析为动态分配的结构数组 (ANSI 89)

转载 作者:太空宇宙 更新时间:2023-11-04 07:46:13 25 4
gpt4 key购买 nike

我正在尝试将 csv 解析为动态分配的结构数组,但是我的尝试因段错误而崩溃。

这是我的数据结构:

SO02773202,5087001,0
SO02773203,5087001,0
SO02773204,5087001,0
SO02773205,5087001,0
SO02773206,5087001,14

这是我将数据解析成的结构:

typedef struct saleslines{
char* salesid;
char* smmcampaignid;
int numberofbottles;
} saleslines_t;

这是我解析文件的尝试:

int read_saleslines(saleslines_t* saleslines, int number_of_lines){
char c;

FILE* fp;
fp = fopen(FILENAME, "r"); /* Open the saleslines file */

if(fp == NULL){ /* Crash if file not found */
printf("Error - file not found\n");
return 0;
}

c = getc(fp);
while (c != EOF){
if (c == '\n'){
number_of_lines += 1;
}
c = getc(fp);
}

printf("Number of lines is %d\n", number_of_lines);

saleslines = (saleslines_t*) malloc((number_of_lines * 2) * sizeof(saleslines_t));

/* allocation of the buffer for every line in the File */
char *buf = (char*) malloc(1000);
char *tmp;

if ( ( fp = fopen(FILENAME, "r" ) ) == NULL )
{
printf( "File could not be opened.\n" );
}
int i = 0;
while (fgets(buf, 255, fp) != NULL){
if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n'))
buf[strlen (buf) - 1] = '\0';

tmp = strtok(buf, ",");
saleslines[i].salesid = strdup(tmp);

tmp = strtok(NULL, ",");
saleslines[i].smmcampaignid = strdup(tmp);

tmp = strtok(NULL, ",");
saleslines[i].numberofbottles = atoi(tmp);

printf("Salesid: %s\nCampaign: %s\nBottles: %i\n\n", saleslines[i].salesid , saleslines[i].smmcampaignid, saleslines[i].numberofbottles);

i++;
}
free(buf);
fclose(fp);
printf("Number of lines is %i\n", number_of_lines);
return number_of_lines;
}

出于某种原因,它解析文件并打印结果结构数组,但是当我之后立即调用此函数时,它因段错误而崩溃:

void print_saleslines_struct(saleslines_t* saleslines, int number_of_lines{
int i;
printf("Number of lines is %i", number_of_lines);
for(i = 0; i < number_of_lines; i++){
printf("Salesid:\t %s\n", saleslines[i].salesid);
printf("Campaign:\t %s\n", saleslines[i].smmcampaignid);
printf("# of Bottles:\t %d\n", saleslines[i].numberofbottles);
}
}

我似乎找不到这个内存错误在哪里。

这里是初始化和main:

saleslines_t* saleslines;
saleslines_summary_t* saleslines_summary;
saleslines_grouped_t* saleslines_grouped;
int number_of_lines = 0;
int* number_of_linesp = &number_of_lines;

/* Main */

int main(){

int chosen_option;

while(1){

printf("What would you like to do?\n");
printf("1. Read saleslines.txt\n");
printf("2. Print saleslines\n");
printf("3. Summarise saleslines\n");
printf("4. Exit the program\n");

scanf("%d", &chosen_option);

switch(chosen_option){

/* case 1 : number_of_lines = read_saleslines_file(saleslines, number_of_lines); break; */

case 1 : number_of_lines = read_saleslines(saleslines, number_of_lines); break;

case 2 : printf("Number of lines is %i", number_of_lines); print_saleslines_struct(saleslines, number_of_lines); break;

case 3 : summarise_saleslines(saleslines, number_of_linesp, saleslines_summary, saleslines_grouped); break;

case 4 : free(saleslines); free(saleslines_summary); free(saleslines_grouped); return 0;

}

}

return 0;

}

更新

问题似乎出在我对结构数组的初始化上。

当我像这样初始化它时:saleslines_t* saleslines;然后像这样进行 malloc:saleslines = malloc(number_of_lines + 1 * sizeof(saleslines_t);

我遇到段错误。

但如果我这样初始化:saleslines[600];(分配的行数超过文件中的行数),一切正常。

我该如何解决这个问题?我希望能够动态分配结构数组中的条目数。

编辑 2

以下是建议的更改:

int read_saleslines(saleslines_t** saleslines, int number_of_lines);

saleslines_t* saleslines;
int number_of_lines = 0;

int main(){

while(1){

printf("What would you like to do?\n");
printf("1. Read saleslines.txt\n");
printf("2. Print saleslines\n");
printf("3. Summarise saleslines\n");
printf("4. Exit the program\n");

printf("Number of saleslines = %i\n", number_of_lines);

scanf("%d", &chosen_option);

switch(chosen_option){

/* case 1 : number_of_lines = read_saleslines_file(saleslines, number_of_lines); break; */

case 1 : number_of_lines = read_saleslines(&saleslines, number_of_lines); break;

case 2 : printf("Number of lines is %i", number_of_lines); print_saleslines_struct(saleslines, number_of_lines); break;

case 3 : summarise_saleslines(saleslines, number_of_linesp, saleslines_summary, saleslines_grouped); break;

case 4 : free(saleslines); free(saleslines_summary); free(saleslines_grouped); return 0;

}

}

return 0;

}

int read_saleslines(saleslines_t** saleslines, int number_of_lines)
{

char c;

FILE* fp;
fp = fopen(FILENAME, "r"); /* Open the saleslines file */

if(fp == NULL){ /* Crash if file not found */
printf("Error - file not found\n");
return 0;
}

c = getc(fp);
while (c != EOF){
if (c == '\n'){
number_of_lines += 1;
}
c = getc(fp);
}

fclose(fp);

printf("Number of lines is %d\n", number_of_lines);

*saleslines = (saleslines_t*) malloc((number_of_lines + 1) * sizeof(saleslines_t));

/* allocation of the buffer for every line in the File */
char *buf = malloc(25);
char *tmp;

if ( ( fp = fopen(FILENAME, "r" ) ) == NULL )
{
printf( "File could not be opened.\n" );
}
int i = 0;
while (fgets(buf, 25, fp) != NULL){
if ((strlen(buf)>0) && (buf[strlen (buf) - 1] == '\n'))
buf[strlen (buf) - 1] = '\0';

tmp = strtok(buf, ",");
(*saleslines)[i].salesid = strdup(tmp);

tmp = strtok(NULL, ",");
(*saleslines)[i].smmcampaignid = strdup(tmp);

tmp = strtok(NULL, ",");
(*saleslines)[i].numberofbottles = atoi(tmp);

printf("Salesid: %s\nCampaign: %s\nBottles: %i\n\n", saleslines[i]->salesid , saleslines[i]->smmcampaignid, saleslines[i]->numberofbottles);

i++;
}
free(buf);
fclose(fp);
printf("Number of lines is %i\n", number_of_lines);
return number_of_lines;
}

程序现在在读取结构数组中的第一个元素后出现段错误。

最佳答案

read_saleslines() 的参数有问题。第一个参数应该是指向结构数组的指针,即双指针。

int read_saleslines(saleslines_t* saleslines, int number_of_lines){

您想修改 saleslines 指向的位置。 saleslines是函数的局部变量,作用域就是那个函数。一旦退出 read_saleslines(),该变量就会被“销毁”,这意味着它所持有的值将无法再访问。添加另一个间接级别,一个指针,您可以修改定义在函数外部 的变量,无论是(丑陋的)全局变量还是其他变量。因此,更改该参数以使函数原型(prototype)匹配

int read_saleslines(saleslines_t** saleslines, int *);

并更改您在函数内部访问它的地方(添加一个*来访问它,例如:

saleslines = (saleslines_t*) malloc((number_of_lines * ...

*saleslines = (saleslines_t*) malloc((number_of_lines * ...

saleslines[i].salesid = strdup(tmp);

(*saleslines)[i].salesid = strdup(tmp);

然后在函数外使用变量的地方添加&:

number_of_lines = read_saleslines(saleslines, number_of_lines);

更改为

some_var = read_saleslines(&saleslines, &number_of_lines);

这将使您的代码工作。

关于c - 将 CSV 解析为动态分配的结构数组 (ANSI 89),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com