hackingcpp / comments

Comments regarding hackingcpp.com
0 stars 0 forks source link

cpp/std/file_streams #35

Open utterances-bot opened 10 months ago

utterances-bot commented 10 months ago

C++ File Input & Output | hacking C++

An introduction to basic file input and output in C++ using standard iostreams.

https://hackingcpp.com/cpp/std/file_streams.html

kooonnn commented 10 months ago

@hackingcpp is.read(reinterpret_cast<char*>(i), sizeof(i)); should be is.read(reinterpret_cast<char*>(i), sizeof(&i)); and so on

XY0797 commented 10 months ago

I agree with Kooonnn's viewpoint, and the test code I wrote below: image os.write(reinterpret_cast<char const*>(i), sizeof(i));should beos.write(reinterpret_cast<char const*>(&i), sizeof(i)); If the above code is not modified, it will cause runtime errors: image It will attempt to read memory address 114514 instead of the memory address where variable i is located, which obviously does not meet our expectations

hackingcpp commented 10 months ago

@kooonnn & @XY0797 -- thanks for spotting the obvious mistake, I'll fix it soon -- the "&" got lost, because "&" is a special html character and I forgot to escape it in the original source code from which the html page is generated -- but @kooonnn also made a little mistake, the &i should be inside the reinterpret_cast and not inside the sizeof