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

时间:2014-02-19 18:19:20

标签: c struct

我必须将一个结构数组的元素复制到新结构数组的空白元素(全部动态分配)。我所拥有的结构数组的每个元素(结构'a')只有两列,left_columnright_column。结构a的每个元素的一些右列条目(例如,我的结构元素)匹配相同结构的下一个(即,第i + 1个)结构元素的左列条目{{ 1}}。我试图找到这样的匹配条目,我试图将匹配元素的整个左右列一个一个地复制到一个较小的,保守的结构,结构a。问题是代码正在编译,但它没有进入最重要的匹配部分,即b部分。它正在进入if循环。

以下是信息和样本数据。由于'PlerumCodeExperientia',这段代码已经到了这个阶段,我感谢他。请建议如何复制这些匹配元素。

感谢你,Dan。

while

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

#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     

第3个元素的片段如下:

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  
等等......,有很多这样的元素。所有这些都包含这些未格式化的两列整数。

如果您有兴趣了解如何将原始文件的内容加载到'structure_a'中,则会给出下一篇文章。它工作正常。

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

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 

} //结束了

1 个答案:

答案 0 :(得分:0)

我必须说,应该有更多的信息,因为问题要完全清楚。 根据给定的数据,这是我的答案。至少我要问的部分是问的。 我希望它有所帮助。

// 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条件。 祝好运。 希望这有帮助。