Why we use reinterpret_cast, while reading from file in c++ | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 12

Why we use reinterpret_cast, while reading from file in c++

25th Apr 2018, 8:51 AM
Wasim Abbasi
Wasim Abbasi - avatar
17 Respostas
+ 13
There is no need to: binary files have no restrictions on what you can read/write: #include <fstream> #include <string> using namespace std::literals; int main(){ std::ofstream("terca.bin", std::ios::binary) << "\0\x1a\xff"s << 5.14; } Where you’re likely to be using reinterpret_cast is when you’re writing the object representation - the actual bytes of RAM used to represent an object in your program. For example, if you want to dump the double with value 5.14 as the 8 bytes it’s made of in memory, and not as the four bytes of its decimal text representation (like I did in the first example): #include <fstream> int main(){ double d = 5.14; std::ofstream("terca.bin", std::ios::binary).write( reinterpret_cast<char*>(&d), sizeof d); } This reinterpretation isn’t really about file I/O, it’s about obtaining the underlying bytes of an object.
25th Apr 2018, 3:54 PM
MsJ
MsJ - avatar
+ 4
Because that's the only way to receive binary input from a file.
25th Apr 2018, 10:35 AM
Timon Paßlick
+ 3
Xander Lim reinterpret_cast<dest_ptr_t>(src_ptr) changes the type of src_ptr to dest_ptr_t without changing anything in the underlying data. In most cases, it's unsafe and you shouldn't use it.
26th Apr 2018, 5:35 AM
Timon Paßlick
+ 2
Because this actually is a reinterpret_cast, but hidden. Look here for more information: https://www.google.com/amp/s/embeddedartistry.com/blog/2017/2/28/c-casting-or-oh-no-we-broke-malloc%3fformat=amp
25th Apr 2018, 3:28 PM
Timon Paßlick
+ 2
Xander Lim Sometimes you need it, though.
26th Apr 2018, 5:38 AM
Timon Paßlick
+ 2
Raj kumar patel Please create a new thread. It will keep this thread on topic and it also helps yourself: More competent people will see the question.
26th Apr 2018, 2:33 PM
Timon Paßlick
+ 1
no its working correctly without using reinterpret_cast
25th Apr 2018, 2:35 PM
Wasim Abbasi
Wasim Abbasi - avatar
+ 1
Binary input? Ok, how?
25th Apr 2018, 3:13 PM
Timon Paßlick
+ 1
this way. while(fileHandler.read((char*)&obj, sizeof(obj)) { obj.showData(); }
25th Apr 2018, 3:24 PM
Wasim Abbasi
Wasim Abbasi - avatar
+ 1
Mohit the coder (M.S.D) Ok, I was unclear, but that's what I meant.
25th Apr 2018, 3:58 PM
Timon Paßlick
+ 1
sachin singh Tell you what?
25th Apr 2018, 8:24 PM
Timon Paßlick
+ 1
Hi
26th Apr 2018, 3:15 AM
PRATEEK Mishra
PRATEEK Mishra - avatar
+ 1
OK. I thought u say I don't need it. now I understand sometimes I still need it.
26th Apr 2018, 5:41 AM
Xander Lim
Xander Lim - avatar
+ 1
My xampp server is not working properly what can I do to fix it
26th Apr 2018, 1:31 PM
Raj kumar patel
Raj kumar patel - avatar
0
what is this?
26th Apr 2018, 5:31 AM
Xander Lim
Xander Lim - avatar
0
OK I won't use it.
26th Apr 2018, 5:37 AM
Xander Lim
Xander Lim - avatar
0
Sometimes we need it
26th Apr 2018, 7:52 AM
Piyush Singh
Piyush Singh - avatar