Checking files in C

I need to check the download files for the game I am creating and I am having problems with it:

I just need to know if the first character is int in some range, if the next one is unsigned short int (checking the type and value of each character), and I could not find a function for this.

Also, I need to check if the nth character is the last character in the file (return an error code if it is not).

The file is binary.

How can i solve this?

Thanks!

E:

This is my boot file function:

Cod_Error load(Info * gameInfo) { FILE * loadFile; unsigned short int dif; int i; randomizeSeed(); gameInfo ->undo = FALSE; loadFile = fopen(gameInfo->fileArchivo, "rb"); fread(&dif, sizeof(dif), 1, loadFile); gameInfo->size = sizeFromDif(dif); gameInfo->undoPossible = FALSE; gameInfo->board = newBoard(gameInfo->size); if(gameInfo->board == NULL) return ERROR_MEM; fread(&(gameInfo->score), sizeof(Score), 1, loadFile); fread(&(gameInfo->undos), sizeof(unsigned short int), 1, loadFile); for(i = 0; i < gameInfo->size; i++) fread(gameInfo->board[i], sizeof(Tile), gameInfo->size, loadFile); fclose(loadFile); return OK; } 
+5
source share
1 answer

Short answer: you cannot.

Long answer:

The binary does not have a specification of what binary data represents. This specification should come from the developer of the program viewing the file. For example, let's take 2 bytes from an arbitrary file.

00101101 00110111

Now, what it is, is completely dependent on the program reading these bytes. You can try to read it as an int and check if it makes sense (iE is in a certain range) if interpreted as int, and you can do the same for any other combination of binary data with any other data type. But you cannot tell what type of data was used to write to the file, as now its just a binary file.

To quote @ M.Oehm:

You cannot read 4 bytes and say "Oh, they are floating."

You can say, however,

"If I consider them floating, are they within the given range I set?"

+2
source

Source: https://habr.com/ru/post/1234614/


All Articles