Need to read 4 bytes into a long int format. Both inFileDrs and outFileDrs are in ios::binary form. Is this the correct way to do this?

The original piece of code was

var tableCount = reader.ReadUInt32();
 writer.Write(tableCount);
unsigned long int tableCount, firstFilePos;
	inFileDrs >> tableCount;
	outFileDrs << tableCount;

Dani AI

Generated

Your streams are opened in binary, but the extraction/insertion operators perform formatted (text) I/O, so they will not copy four raw bytes reliably. For a true 4-byte copy you should use a fixed-width 32-bit type and the unformatted read/write functions.

Use a std::uint32_t and read/write the raw bytes:

#include <cstdint>

std::uint32_t tableCount = 0;
if (!inFileDrs.read(reinterpret_cast<char*>(&tableCount), sizeof(tableCount))) {
    // handle short read or stream error
}
outFileDrs.write(reinterpret_cast<const char*>(&tableCount), sizeof(tableCount));

Also add a compile-time check that a uint32_t is actually 4 bytes on your platform with static_assert(sizeof(std::uint32_t) == 4);. See std::istream::read and std::ostream::write for details (read, write) and the fixed-width integer types (cstdint).

Notes drawn from the thread: , opening in binary mode was correct; , initializing variables is good practice but if you immediately overwrite a variable with read() it does not need a prior value — however always check the stream after reading because a failed read leaves the value indeterminate. Also check that both files opened successfully before I/O.

Portability caveats: avoid unsigned long for a 4-byte assumption (its size varies by platform). Confirm the file byte order (endianness) of the data source; if the producer used a different endianness, swap bytes after reading (C++20 exposes platform endianness via std::endian).

Recommended Answers

All 2 Replies

The code you've posted is both technically and syntactically valid. Whether it works properly or not depends on how you've defined inFileDrs and outfileDrs and how you use tableCount and firstFilePos elsewhere in your code.

My only concern is that you didn't initialize tableCount and firstFilePos, you only declared them. Others will debate this statement, but it's not really wrong. It is, however, not very reliable and definitely not advisable. You really should initialize EVERY variable when you declare it. That way, you know where you're starting with it and aren't hoping it's uninitialized value will be useful to you.

Ok, i will intialize them, here is how i opened up the infile and outfile

At the moment I want to be able to just take the pieces of this file and read them and maybe do some edits, i am not sure how the integrity of the program will work, but if u think i did this right syntactically then I am hoping it will work.

ifstream inFileDrs(_orgDrsPath, std::ios::in | std::ios::binary);	
ofstream outFileDrs(newDrsName, std::ios::out | std::ios::binary);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.