-2

It prints one thing if its run on my computer, and a second thing if its ran on any other computer.

#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 );
    }
}

this is on a server so each person connects to the server, has the same .c file, compiles and runs it.

when I run it I get

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

test
Segmentation fault

when I run it on my phone or when my friend runs it we get:

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

test
Segmentation fault

It is like this with other programs that do simpler things as well, like a program that just prints("Hello World\n") will literally do nothing when I run it and will print normally on every other computer, no idea what's going on.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • _Questions seeking debugging help (why isn't this code working?) must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]._ – Sourav Ghosh Oct 11 '17 at 06:27
  • Did you notice you never used `printThisFile()`? – Sourav Ghosh Oct 11 '17 at 06:28
  • Where's your error checking? What if not enough arguments are passed to the program? What if the program fails to open the file? What if you fail to read? – Some programmer dude Oct 11 '17 at 06:30
  • What are the differences between the two systems? What is your input? – Bart Friederichs Oct 11 '17 at 06:31
  • I have been using c for a solid 3 days, I have no idea what i'm doing – sam whittle Oct 11 '17 at 06:32
  • And if it also happens with simpler programs, why not post those? – Bart Friederichs Oct 11 '17 at 06:32
  • Furthermore `structArray[...].f_name = tempHW5struct.f_name` is wrong. It will make *all* `f_name` pointer point to the very same `tempHW5struct.f_name`. – Some programmer dude Oct 11 '17 at 06:32
  • "I have been using c for a solid 3 days, I have no idea what i'm doing", then please stop asking complex questions here and learn C first. There are many online and offline resources for that. – Bart Friederichs Oct 11 '17 at 06:32
  • If you're that new you're way in over your head I'd say! Take a few steps back, [get a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and *start over*. – Some programmer dude Oct 11 '17 at 06:36
  • Have a look at the following: [Why struct alignment different in iOS](https://stackoverflow.com/questions/34715726/why-struct-alignment-different-in-ios) – grek40 Oct 11 '17 at 06:38
  • this is my programming homework guys, like, i'm a couple days into a class that is supposed to teach me c and this is the homework, my teacher hasn't gone over any of this and i'm just desperately trying to understand. – sam whittle Oct 11 '17 at 06:38
  • Basically, as far as the homework is concerned, **do not read a binary file cross-platform**, instead connect to the official server for all your development activities. Cross-platform differences are nothing to be approached on the first homework – grek40 Oct 11 '17 at 06:44
  • Alright I will keep that in mind, thank you. – sam whittle Oct 11 '17 at 06:52
  • You shouldn't need any of those `fseek` calls - the position within the file gets updated when you read/write from/to the file. – Chris Turner Oct 11 '17 at 09:49

1 Answers1

1

You never allocate memory for structArray, or make it point anywhere valid. It is uninitialized and will point to an indeterminate (and seemingly random) location.

Then when you dereference it to write to some seemingly random location you will have undefined behavior.

Since you program in C you could use variable-length arrays like

HW5_struct_updated structArray[originalDataFileSize  / sizeof(HW5_struct)];
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • So first off thank you, It prints the file correctly now, it assigns the variables into the array correctly, you fixed it! I am very new so i really just don't understand pointers/arrays well enough. – sam whittle Oct 11 '17 at 06:49