0

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

文件 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

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

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

如果你制定一个 C 代码来解决这个问题,那将是非常有帮助的。谢谢。

4

3 回答 3

1

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

  • 您需要使用将使用的结构格式化每个文件,在第一个文件中结构 [1] 您只需要一个成员名称(char 数组),在第二个结构 [2] 中您需要三个成员名称(char 数组) 和 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 );
    }
    
于 2012-11-18T12:51:22.483 回答
0

我正在尝试执行此程序,但它无法运行。

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

int main()
{
    FILE *fp1 ,*fp2;
    char ca , cb;
    int i,j,x,s;
    char A[i][j],B[i][j];
    char file1[1024], file2[1024];

    printf( "Enter file1: ", file1 );
    gets( file1 );
    printf( "Enter file2: ",file2 );
    gets( file2 );
    fp1=fopen( file1, "r");

    if ( fp1 == NULL ){
        printf("Cannot open %s for reading \n", file1 );
        exit(1);
    }

    while ( ( fgets( file1, sizeof file1, stdin ) ) != EOF ){
        for(i=0;i<2500;i++){
            for(j=0;j<5;j++){
                fscanf(fp1,"%s",&A[i][j]);
            }
        }
    }

    fp2 = fopen( file2, "r");

    if ( fp2 == NULL ){
        printf("Cannot open %s for reading \n", file2 );
        exit( 1 );
    }

    while ( fgets ( file2, sizeof file2, stdin) != EOF){
        for( i = 0; i < 2500; i++ ){
            for( j = 0; j < 5; j++ ){
                fscanf(fp2,"%s",&B[i][j]);
            }
        }
    }
    /*Here it was a ; without any sense (after the if)*/
    if( strcmp( A[i][j], B[i][j] ) ) == 0;
    {
        printf( "%s /n %s  ", strcmp( A[i][j], B[i][j] ) );
    }

    fclose(fp1);
    fclose(fp2);

    return 0;
}

编译器在“==”标记之前显示语法错误(strcmp 函数底部的第 8 行)。有什么帮助吗??

于 2012-11-19T22:29:14.593 回答
0

你在 if 后面加了一个分号,却忘记了括号。

if( strcmp( A[i][j], B[i][j] ) == 0 ){
        printf( "%s /n %s  ", strcmp( A[i][j], B[i][j] ) );
}
于 2012-11-19T22:40:27.940 回答