-1

I have a data file in binary where the first four bytes are some integer that I want to read. I simply do:

int * num_rounters_p = malloc(sizeof(int));
fread(num_rounters_p, 4, 1, p_file);

printf("%d\n", *num_routers_p); // 10

This works fine (and please tell me if it doesn't!), however I do know the size of this particular value, and so it isn't really necessary to store it dynamically.

Is it possible to do something like

int x = some_read_function(4, 1, p_file);

printf("%d\n", x); // 10

Basically storing the value on stack instead of the heap? The code example above is of course not grounded in C, but I hope I got my point across :))

Snusifer
  • 485
  • 3
  • 17

2 Answers2

2

The most straightforward way would be

int num_routers;
size_t items_read = fread( &num_routers, sizeof num_routers, 1, p_file );

if ( items_read < 1 )
{
  // read error, handle as appropriate
}
else
{
  // do something with num_routers
}

An int is not guaranteed to be 4 bytes wide, it's only guaranteed to be at least 2 bytes wide, so it's safer to use sizeof num_routers than a literal 4. Of course, that assumes that the binary file was written on the same platform that you're reading from.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

Nvm! Easy fix. Just do:

int num_routers;
fread(&num_routers, sizeof(int), 1, p_file);
Snusifer
  • 485
  • 3
  • 17
  • 1
    Better with `sizeof num_routers` – klutt Mar 04 '21 at 15:57
  • 1
    All input functions deserve a check of the return value. – chux - Reinstate Monica Mar 04 '21 at 15:57
  • Input functions often fail to read expected data. `fread()` deserves a test to see if the return value is acceptable. i.e Was the expected amount of data truly read? Else `printf("%d\n", *num_routers_p);` may print misleading data. – chux - Reinstate Monica Mar 05 '21 at 00:21
  • @chux-ReinstateMonica how can input functions fail to read data you tell them to read?? – Snusifer Mar 05 '21 at 00:27
  • "how can input functions fail to read data you tell them to read" --> Many reasons. (corrupt data, invalid data, loss of communication, scant data, delays, ...) Sounds like a good question to post for a complete answer. – chux - Reinstate Monica Mar 05 '21 at 00:59
  • @Snusifer Because that's MY name in an orchestra I play in. Here is a picture of my uniform. https://ibb.co/MGzFDNj – klutt Mar 05 '21 at 08:03
  • @klutt Wow! What a coincidence. I wonder why they went for that name, and what it means in that context. Dang, I want that uniform now xD – Snusifer Mar 05 '21 at 13:21
  • @Snusifer It has the exact same etymology in my case. It's because I'm using snus. Actually I'm also making it. But the uniform and name is mine. Join the orchestra, and you'll get an own uniform, but the name is already taken. ;) – klutt Mar 05 '21 at 13:29
  • @Snusifer Which country? Also Sweden? – klutt Mar 05 '21 at 13:31
  • @klutt Norway x)) Whoever founded your orchestra probably thought of the same pun, since you're from Sweden. In that regard, its crazy to name an orchestra Snusifer tbh... – Snusifer Mar 05 '21 at 13:48
  • @Snusifer Oh, it's not the name of the orchestra. It's my nickname in the orchestra. The name of the orchestra is Wijkmanska Blecket – klutt Mar 05 '21 at 13:53
  • @klutt so we thought of the same pun? xDD nice. wise men think alike – Snusifer Mar 05 '21 at 14:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229563/discussion-between-klutt-and-snusifer). – klutt Mar 05 '21 at 14:06