質問

I am writing a very simple program in C++, to copy the bootsector from one file to another. My code is:

#include <fstream>

int main ()
{
    char buffer[512];
    std::fstream myfile ("boot.bin", std::ios::binary || std::ios::in);

    myfile.seekg(0);
    myfile.read(buffer, 512);
    myfile.close();

    myfile.open("boot.img", std::ios::binary || std::ios::out);
    myfile.seekp(0);
    myfile.write(buffer, 512);
    myfile.close();

    return 0;
}

However, when I try to compile, I get an error:

Link:
  Generating code
  Finished generating code
Bootcopy.vcxproj -> c:\users\xxx\documents\visual studio 2010\Projects\Bootcopy\Release\Bootcopy.exe
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(562,5): error MSB6006: "mt.exe" exited with code 31.

I've googled for this error, but I only found solutions that didn't work for me. The weird thing is that I am able to compile the code if I rebuild the solution (Build > Rebuild Solution or CTRL + ALT + F7). When I compile normally, I get either a "build failed" (if this is the first build after a rebuild), or a "build succeeded" (if I changed some things around).

Also, the resulting executable doesn't seem to work. The idea is to copy the first 512 bytes from boot.bin to boot.img. The bytes seem to read correctly (I put "cout << buffer" in my code, and the string was printed correctly up to the first null character), but are not written to boot.img. When I change boot.img to a non-existent file, there is no file created. When I run the program in the debugger, I don't get any error (even if both files are non-existent in the project directory).

役に立ちましたか?

解決

std::ifstream myfile("boot.bin", std::ios::binary || std::ios::in);

Is "||" allowed?

check with

"|" single pipe

std::ifstream myfile("boot.bin", std::ios::binary | std::ios::in);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top