Add an implementation, still just trying to read
This commit is contained in:
68
Assignment9/documentation/EndiannessAndHomework9.c
Normal file
68
Assignment9/documentation/EndiannessAndHomework9.c
Normal file
@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <arpa/inet.h>
|
||||
// FILE format is: 'header', then 2023 skipped bytes, then the number of interest
|
||||
#define OURINT 1426915328
|
||||
#define OFFSETTODATA 2023
|
||||
|
||||
int writeToFile(){
|
||||
|
||||
FILE* fp = fopen("toReadLater.txt","w+");
|
||||
char c = 'h';
|
||||
fwrite(&c, sizeof(char), 1, fp);
|
||||
c = 'e';
|
||||
fwrite(&c, sizeof(char), 1, fp);
|
||||
c = 'a';
|
||||
fwrite(&c, sizeof(char), 1, fp);
|
||||
c = 'd';
|
||||
fwrite(&c, sizeof(char), 1, fp);
|
||||
c = 'e';
|
||||
fwrite(&c, sizeof(char), 1, fp);
|
||||
c = 'r';
|
||||
fwrite(&c, sizeof(char), 1, fp);
|
||||
unsigned int buf = OFFSETTODATA;
|
||||
fwrite(&buf, sizeof(int), 1, fp);
|
||||
fseek(fp, OFFSETTODATA, SEEK_CUR);
|
||||
|
||||
buf = OURINT;
|
||||
fwrite(&buf, sizeof(int), 1, fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
int readFromFile(){
|
||||
FILE *fp = fopen("toReadLater.txt", "r+");
|
||||
fseek(fp, 6 , SEEK_SET);
|
||||
int skip = 0;
|
||||
fread(&skip, sizeof(int), 1, fp);
|
||||
// NOTE, in HW, the size of the section is a value in
|
||||
// Big-Endian format, it will actually need conversion
|
||||
fseek(fp, skip, SEEK_CUR);
|
||||
unsigned int qBe = 0;
|
||||
unsigned int qLe = 0;
|
||||
int i;
|
||||
unsigned char *c = (char *)(&qBe);
|
||||
unsigned char *d = (char *)(&qLe);
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
c[i] = getc(fp);
|
||||
printf("Byte %i: %i %x\n",i, c[i], c[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
d[4-i-1] = c[i];
|
||||
}
|
||||
|
||||
printf("Translated using swapping the bytes order: %i\n", qLe);
|
||||
qLe = ntohl(qBe);
|
||||
printf("Translated using ntohl: %i\n", qLe);
|
||||
fclose(fp);
|
||||
}
|
||||
int main (int argc, char** argv){
|
||||
unsigned int wantToRead;
|
||||
writeToFile();
|
||||
printf("Sizeof (int) is:%li\n", sizeof(unsigned int));
|
||||
readFromFile();
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user