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.