gpt4 book ai didi

在 C 中将一个结构数组动态复制到另一个结构数组

转载 作者:行者123 更新时间:2023-11-30 17:39:13 25 4
gpt4 key购买 nike

我必须将一个结构数组的元素复制到新结构数组的空白元素(全部动态分配)。我拥有的结构数组(结构“a”)的每个元素都只有两列:left_columnright_column。结构 a 的每个元素(例如,第 i 个结构元素)的某些右列条目与该结构的下一个(即第 i+1 个)结构元素的左列条目相匹配结构a。我试图找到这样的匹配条目,并尝试将匹配元素的整个左列和右列逐一复制到一个更小的、保守的结构,结构b。问题是代码正在编译,但它没有进入最重要的匹配部分,即 if 部分。它正在进入 while 循环。

下面保留的是信息和示例数据。由于“PlerumCodeExperientia”,这段代码才走到了这个阶段,我感谢他。请建议如何复制这些匹配元素。

谢谢你,丹。

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

int main(int argc, char* args[])
{
struct a
{
int left_line;
int right_line;
};

struct b
{
int conserved_left;
int conserved_right;
};

FILE *fp100; // Output File

fp100 = fopen("Conserved_Elements.txt", "a");

struct a *ptr1;
int structACapacity = 3; // Only 3 such comparison files are being worked with, there are >1000 comparison files
ptr1 = malloc(structACapacity*sizeof(struct a));

struct b *ptr2;
int structBCapacity = 1000;
ptr2 = malloc(structBCapacity*sizeof(struct b));

int structure_ctr;
int number_of_line_comparison_files = 3; // Only 3 for the time being
int knt;
int left, right;

for (structure_ctr=0; structure_ctr < number_of_line_comparison_files; structure_ctr++) {

knt = 0;

while (((ptr1+knt) < (ptr1+structACapacity-1)) && (knt < 500)) {
fprintf(fp100, "Getting Into While\n");

// finding the matching entries between right column of knt and left column of (knt+1)
if ((ptr1+knt)->right_line == (ptr1+(1+knt))->left_line) {
fprintf(fp100,"\tGetting Into the If\n");

// copying matching values to the struct b
left = (ptr2+knt)->conserved_left = (ptr1+knt)->left_line;
right = (ptr2+knt)->conserved_right = (ptr1+knt)->right_line;

//fprintf(fp100,"C-Left:%d\tC-Right:%d\tLeft%d\tRight%d\n",(ptr2+knt)->conserved_left,(ptr2+knt)->conserved_right,left,right);
// left, right are there for convinience only - easier to see, same values
fprintf(fp100,"C-Left:%d\tC-Right:%d\n", left,right);
}

++knt;
} // end of while
} //end of for
}

文件看起来:第一个结构元素的片段如下所示:

17   216

26 119

28 16

29 122

59 124

60 116

62 114

63 112

66 61

69 54

70 51

71 62

91 40

99 38

第二个元素的片段如下所示:

321    25

110 45

116 49

216 110

56 117

54 131

32 167

31 178

8 188

12 199

39 239

60 244

121 263

124 275

第三个元素的片段如下所示:

75    223

61 248

45 278

31 290

10 291

111 311

117 324

128 338

139 347

148 365

167 376

178 381

191 394

193 397

等等...,这样的元素还有很多。它们都包含这样未格式化的两列整数。

如果您有兴趣了解原始文件的内容是如何加载到“structural_a”中的,我们将给出下一部分。效果很好。

for(q=0; q < number_of_line_comparison_files; q++)//遍历文件总数

{

// File Name Etc ..

while (fgets(file_line,99,line_comparison_file)!= NULL)

{

++number_of_lines ; // Integer


for(j=0;j<6;j++)

string_left_line[j]=file_line[j];


for(j=0;j<6;j++)

string_right_line[j]=file_line[6+j];


left_line = atoi(string_left_line);

right_line = atoi(string_right_line);

*(&(ptr1+q)->left_line) = left_line;

*(&(ptr1+q)->right_line) = right_line;

fprintf(fp100,"Left:%d\tRight:%d\n",(ptr1+q)->left_line,(ptr1+q)->right_line);


} // END OF THE 'WHILE'


fprintf(fp100,"\n\n\t================================== ================================\n\n");

}//For 结束

最佳答案

我必须说,为了让问题完全清楚,应该有更多的信息。根据给定的数据,这就是我的答案。至少我假设的部分被问到了。我希望它有帮助。

// copy_pointer_struct.c

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

int main(int argc, char* args[])
{
struct a
{
int left_line;
int right_line;
};

struct b
{
int conserved_left;
int conserved_right;
};

FILE *fp100;
fp100 = fopen("struct_file.txt", "a");

struct a* ptr1;
int structACapacity = 3;
ptr1=(struct a*)malloc(structACapacity*sizeof(struct a));

struct b *ptr2;
int structBCapacity = 1000;
ptr2=(struct b*)malloc(structBCapacity*sizeof(struct b));

// for writing test values to ptr1 array
int i;
for ( i = 0; i < structACapacity; i++ )
{
ptr1[i].left_line = i;
(ptr1+i)->right_line = i+1;
}

int structure_ctr,
number_of_line_comparison_files = 10,
knt;

// This outer for is neccessary only if you are getting structs form another source - a file for example or another structure.
//for(structure_ctr=0; structure_ctr < number_of_line_comparison_files; structure_ctr++)
{
// this inner for would be neccessary if you had an 2 dimensional array of structs or a pointer to a pointer to struct.
//for(knt=0; knt<500; knt++)
{
knt = 0;
// in your while condition there is no changing value, (change being made inside cycle or condition itself),
// which results in an infinite (for condition true) cycle or 0 cycles (for condition false)

// this particular condition works until we have reached 500th element or the end of array,
// whichever has lower value.
// Of course in case of use of outer for ( && knt < 500) wouldn't be used, depending of the goal you have.
while( (ptr1+knt) < (ptr1+structACapacity-1) && knt < 500 )
{
if( (ptr1+knt)->right_line == (ptr1+1+knt)->left_line ) // comment line for file test
{
// copying duplicate values to the struct b
int left = (ptr2+knt)->conserved_left = (ptr1+1+knt)->left_line;
int right = (ptr2+knt)->conserved_right = (ptr1+knt)->right_line;

// here you can write those values to a file as 2 integers, for example
fprintf(fp100,"%d %d\n", left, right);
}
++knt;
} // while
}//
} //for structure_ctr
fclose(fp100);
}

也许这个添加有帮助:

// copy_pointer_struct.c

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

typedef struct a
{
int left_line;
int right_line;
} structA;

typedef struct b
{
int conserved_left;
int conserved_right;
} structB;

int fillColumnPtr(FILE* source, structA* ptr);



int main(int argc, char* args[])
{


FILE *fp100;
fp100 = fopen("struct_file.txt", "w");

structA* ptr1;
int structACapacity = 1000;
ptr1=(structA*)malloc(structACapacity*sizeof(struct a));

printf("kapacitet ptr1: %d\n", sizeof ptr1);

structB *ptr2;
int structBCapacity = 1000;
ptr2=(struct b*)malloc(structBCapacity*sizeof(struct b));


char *filename = "columns_file4.txt";
FILE *cfile1 = fopen(filename, "r");

printf("Loading values from file: %s . . . \n", filename);

int number_of_lines = fillColumnPtr(cfile1, ptr1);

fclose(cfile1);
printf("Done.\n\n");
printf("Values loaded to ptr1(total lines %d):\n", number_of_lines);

int i;
for ( i = 0; i < number_of_lines; i++ )
{
printf("left: %6d, right: %6d\n", ptr1[i].left_line, ptr1[i].right_line);
}

printf("\nProcessing...\n");
int structure_ctr,
number_of_line_comparison_files = 10,
knt;

knt = 0;

while( (ptr1+knt) < (ptr1+structACapacity-1) && knt < number_of_lines )
{
if( (ptr1+knt)->right_line == (ptr1+1+knt)->left_line ) // comment line for file test
{
// copying duplicate values to the struct b
int left = (ptr2+knt)->conserved_left = (ptr1+1+knt)->left_line;
int right = (ptr2+knt)->conserved_right = (ptr1+knt)->right_line;

// here you can write those values to a file as 2 integers, for example
fprintf(fp100,"%d %d\n", left, right);
printf("%d %d\n", left, right);
}
++knt;
} // while
fclose(fp100);
}

// function: transfers data from a source file to pointer to struct
int fillColumnPtr(FILE* source, structA* ptr)
{
char file_line[20],
*str_left_line = calloc(sizeof(char), 6),
str_right_line[6];
int left_line, right_line, j, line_counter = 0, i = 0;
while ( fgets(file_line, 19, source) != NULL )
{
printf(">> fileline: %s", file_line);
for(j=0;j<6;j++)
{
str_left_line[j]=file_line[j];
str_right_line[j]=file_line[6+j];
}
left_line = atoi(str_left_line);
right_line = atoi(str_right_line);
printf(" left right: %-3d %6d\n", left_line, right_line);
ptr[i].left_line = left_line;
ptr[i].right_line = right_line;
++i;
++line_counter;
}
return line_counter;
}

注意:我检查了一些应该位于文件中的源值 - 它们都不满足 if 内的条件,导致空指针。附加的帖子,我已经用改变的值进行了测试并且它有效。对于此方法,请检查行间距。
“columns_file4.txt”的内容:

321    25
25 45
116 49
216 110
110 117
54 131
32 167
31 178
178 188
12 199
199 239
60 244
121 263
124 275

//文件结束
在此文件中存在相同的值,例如:
(第 1 行右列)25 ==(第 2 行左列)25、110、178、199。如果这不是您想要的,请更改 if 条件。祝你好运。希望这有帮助。

关于在 C 中将一个结构数组动态复制到另一个结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21889042/

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