我的C程序打印出两个不同的东西

时间:2017-10-11 06:27:09

标签: c string pointers io binary

如果它在我的计算机上运行会打印一件事,如果它在任何其他计算机上运行则会打印第二件事。

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

typedef struct hw5_struct {
    char f_name[12];
    char l_name[12];
    int age;
    float height;
}HW5_struct;

typedef struct hw5_struct_updated {
    char *f_name;
    char *l_name;
    char *address;
    int age;
    int birthday;
    float height;
}HW5_struct_updated;

void printThisFile( FILE *file, int fileSize );
void putNewStructuresInFile( FILE *newFile, int matchingFileSize );

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

    printf("\n");

    const char *fileLocation = argv[1];

    FILE *originalData = fopen( fileLocation, "rb" );

    fseek( originalData, 0L, SEEK_END );
    int originalDataFileSize = ftell( originalData );
    rewind( originalData );
    printf( "The size of the original file is: %d\n", originalDataFileSize );

    HW5_struct_updated *structArray;

    printf("test\n");

    int i = 0;
    HW5_struct tempHW5struct;

    for( i = 0 ; i < originalDataFileSize ; i += sizeof( HW5_struct ) ) { 
        fseek( originalData, i, SEEK_SET );
        fread( &tempHW5struct, sizeof( HW5_struct ), 1, originalData);
        printf( "f_name: %s\nl_name: %s\nage: %d\nheight: %f\n\n", tempHW5struct.f_name, tempHW5struct.l_name, tempHW5struct.age, tempHW5struct.height );
        printf("test\n");
        structArray[ i / sizeof( HW5_struct ) ].f_name = tempHW5struct.f_name;

        //strcpy( structArray[ i / sizeof( HW5_struct ) ].f_name, tempHW5struct.f_name );

        printf("%s\n", structArray[ i / sizeof( HW5_struct ) ].f_name);
        //structArray[ i / sizeof( HW5_struct ) ].l_name = tempHW5struct.l_name;
        structArray[ i / sizeof( HW5_struct ) ].age = tempHW5struct.age;
        structArray[ i / sizeof( HW5_struct ) ].height = tempHW5struct.height;
        structArray[ i / sizeof( HW5_struct ) ].birthday = 1;
        //structArray[ i / sizeof( HW5_struct ) ].address = "8008 Mulberry Ln";
        printf("structure #%d\n was read", ( i / sizeof( HW5_struct ) ) );
    }   

    FILE *newData = fopen( "my_temp.tmp", "wb");

    for( i = 0 ; i < originalDataFileSize ; i += sizeof( HW5_struct ) ) { 
        fseek( originalData, i, SEEK_SET );
        fread( &tempHW5struct, sizeof( HW5_struct ), 1, originalData );
        printf( "f_name: %s\nl_name: %s\nage: %d\nheight: %f\n\n", tempHW5struct.f_name, tempHW5struct.l_name, tempHW5struct.age, tempHW5struct.height );
    }

    HW5_struct_updated tempHW5structUpdated;

    for( i = 0; i < originalDataFileSize ; i += sizeof( HW5_struct ) ) {
        fseek(newData, i * sizeof( HW5_struct_updated ), SEEK_SET);
        fwrite( &tempHW5structUpdated, sizeof( HW5_struct_updated ), 1, newData);
    }

    return 0;
}

void printThisFile( FILE *file, int fileSize) {
    int i = 0;
    HW5_struct tempStruct;

    for( i = 0 ; i < fileSize ; i += sizeof( HW5_struct ) ) {
        fseek( file, i, SEEK_SET );
        fread( &tempStruct, sizeof( HW5_struct ), 1, file);
        printf( "f_name: %s\nl_name: %s\nage: %d\nheight: %f\n\n", tempStruct.f_name, tempStruct.l_name, tempStruct.age, tempStruct.height );
    }
}

这是在服务器上,所以每个人都连接到服务器,具有相同的.c文件,编译并运行它。

当我跑它时,我得到了

The size of the original file is: 320
test
f_name: Fred
l_name: Hutcheson
age: 32
height: 6.000000

test
Segmentation fault

当我在手机上运行或当我的朋友运行它时,我们得到:

The size of the original file is: 320
test
f_name: Fred
l_name: cheson
age: 1920098636
height: 0.000000

test
Segmentation fault

就像这样,其他程序也做了更简单的事情,就像一个程序只有prints("Hello World\n")在我运行它时会毫无作用,并且会在每台其他计算机上正常打印,不知道发生了什么。

1 个答案:

答案 0 :(得分:1)

您永远不会为structArray分配内存,也不会指向任何有效的内存。它是未初始化,并指向不确定(以及看似随机)的位置。

然后,当您取消引用它以写入一些看似随机的位置时,您将拥有undefined behavior

由于您使用C编程,因此可以使用variable-length arrays之类的

HW5_struct_updated structArray[originalDataFileSize  / sizeof(HW5_struct)];