gpt4 book ai didi

比较和检查两个文件中的列

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

我有两个文件,如下所示:

文件 A 是:

J5
J15
J25
J30
J35

文件 B 是:

J0 23 56
J5 24 58
J10 26 60
J15 29 63
J20 31 36
J25 23 32
J30 51 14
J35 34 21
J40 46 12

问题是我必须检查文件 A 和文件 B 的内容,并将第 J 个项目的值复制到新文件中,如下所示:(文件 C ):

J5 24 58
J15 29 63
J25 23 32
J30 51 14
J35 34 21

如果你编写一个 C 代码来解决这个问题,那将会有很大的帮助。谢谢。

最佳答案

我建议您制作三个“随机访问文件”(因为它们更有组织性),但首先:

  • 您需要使用以下结构格式化每个文件,它将使用,在第一个文件中,结构 [1] 您只需要一个成员名称(字符数组),在第二个结构 [2] 中,您需要三个成员名称(字符数组)和 2 个值(我想是两个整数),第三个限制与第二个相同,因此您可以重新利用它。

[1]

/* Struct for the first file. */
typedef {
char name[4];
} file1_t;

[2]

/* Struct for the second file. */
typedef {
char name[4];
int value1, value2;
} file2_t;
  • 要创建这些文件,您必须使用如下代码(假设文件以“wb”模式打开,但未创建):

    /* This is an incomplete example (I can't make your whole Homework)*/

    void file2Creator( FILE *fPtr )
    {
    int i; // Counter to create the file.
    file2_t data = { "", 0, 0 }; // A blank example to format the file.

    /* You will create 9 consecutive records*/
    for( i = 1; i <= 9; i++ ){
    fwrite( &data, sizeof( file2_t ), 1, fPtr );
    }

    fclose( fPtr ); // You can close the file here or later however you need.
    }
  • 之后,您必须填写您所做的空白(我想文件已打开):

    void fillFile2( FILE *fPtr)
    {
    int position;
    file2_t data = { "", 0, 0 };


    printf( "Enter the position to fill (1-9) 0 to finish:\n?" );
    scanf( "%d", &position );

    while( position != 0 ){
    printf( "Enter the name, and the two other values (integers):\n?" );
    fscanf( stdin, "%s%d%d", data.name, data.value1, data.value2 );

    /* You have to seek the pointer. */
    fseek( fPtr, ( position - 1 ) * sizeof( file2_t ), SEEK_SET );
    fwrite( &data, sizeof( file2_t ), 1, fPtr );
    printf( "Enter a new position (1-9) 0 to finish:\n?" );
    scanf( "%d", &position );
    }

    fclose( fPtr ); //You can close the file or not, depends in what you need.
    }
  • 现在假设你已经格式化了文件1、文件2和文件3,并且文件1和2都被填充了,你想填充文件3怎么办?简单。

    /* 3 pointers to each file */
    void fillFile3( FILE *f1Ptr, FILE *f2Ptr, FILE *f3Ptr )
    {
    int i, j, k, number;
    char word[ 4 ];
    file1_t data1 = { "" };
    file2_t data2, data3 = { "", 0, 0 };

    k = 1;

    /* I suppose that files are opened with their correctly FILE pointers. */
    for( i = 1; i <= 5; i++ ){

    /* I locate in the "i" position of the file1. */
    fseek( f1Ptr, ( i - 1 ) * sizeof( file1_t ), SEEK_SET );

    /* I read the slot. */
    fread( &data1, sizeof( file1_t ), 1, f1Ptr );

    /*Now we compare the file2 until we find the correct value, if it is organized we can jump some slots if not compare with all.*/
    for( j = 1; j <= 9, j++ ){
    fseek( f2Ptr, ( j - 1 ) * sizeof( file2_t ), SEEK_SET );
    fread( &data2, sizeof( file2_t ), 1, f2Ptr );

    /* We compare the strings, If they are equal we paste the value in file 3*/
    if( strcmp( data1.name, data2.name ) == 0 ){

    /*file 3 is of type of the file 2*/
    fseek( f3Ptr, ( k - 1 ) * sizeof( file2_t ), SEEK_SET );
    fwrite( &data2, sizeof( file2_t ), 1, f3Ptr );
    k++;
    break;
    }
    }
    }
    fclose( f1Ptr );
    fclose( f2Ptr );
    fclose( f3Ptr );
    }

关于比较和检查两个文件中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13438941/

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