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

时间:2012-11-18 09:27:08

标签: c

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

文件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项的值复制到一个新文件中:(FILE C):

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

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

3 个答案:

答案 0 :(得分:1)

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

  • 你需要用结构格式化每个文件它会在第一个文件中使用结构[1]你只需要一个成员名称(char数组),在第二个结构[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.
    }
    
  • 现在我想你已经格式化了file1,文件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 );
    }
    

答案 1 :(得分: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;
}

编译器显示语法错误befor“==”令牌(strcmp函数底部的第8行)。任何帮助?

答案 2 :(得分:0)

你在if之后添加了一个分号,并忘记了一个括号。

if( strcmp( A[i][j], B[i][j] ) == 0 ){
        printf( "%s /n %s  ", strcmp( A[i][j], B[i][j] ) );
}