开发者

How to open floppy with fstream

How to open floppy disc开发者_JAVA技巧 with fstream? I'm trying something like this: but it always returns error

#include <iostream>
#include <fstream>

using namespace std;

char a='k';

int main()
{
    fstream stream;
    stream.open( "\\\\.\\A:", ios::binary );

    if( stream.good() == false )
    {
            cout <<"Error";
    }

    for( int i = 0 ; i < 512 ; i++ )
    {
            stream >> a;
            //cout << a;
    }
    stream.close();
    cin.get();
    return 0;
}


You cannot use fstream to open a device - only a file within the filesystem contained on that device. You need to use operating system specific functionality to access a device.

EDIT: To be clear, it might be possible to open the floppy device using fstream but this level of access to the system goes beyond the level of abstraction provided by the Standard C++ Library and so OS-specific functions should be used instead.


As described in the Remarks section of this MSDN documentation, device files should be opened with FILE_SHARE_READ|FILE_SHARE_WRITE sharing mode. By default, fstreams do not support this. You will need to directly open a handle to the file using the low-level win32 CreateFile API, then read/write using ReadFile and WriteFile. When done, close the handle with CloseHandle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜