热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CreateFile打开设备I/O问题

我在VS2010中用MFC写一个硬盘读写测试遇到一个问题如下HANDLEhFILE(_T(\\.\C:),GENERIC_WRITE,FILE_SHARE_READ,NULL,O
我在VS2010中用MFC写一个硬盘读写测试遇到一个问题
如下
HANDLE hFILE = (_T("\\.\C:"),GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

上边这一段在以只写的方式打开设备程序中编译运行是没有问题的。
假如我获取到的盘符为D:我需要传给CreateFile第一个参数一个变量(就是我另一个控件获取到盘符的变量)
不管我怎么写一直在报错
比如我如下这么写

CString n = disk[drive];
HANDLE hFILE = (_T("\\.\n:"),GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

disk[]是存放盘符的数组 dirve下标。如上这么写就会报错
各位帮帮忙看一下  谢谢

19 个解决方案

#1


第一个参数中(“\\.\%c”,n)报错,实参过多

#2


你要把变量n用CString的Format组合字符串。

#3


("\\.\n:") 这个 n 就是 'n' 而不是变量 CString n

#4


比如CString n = 

#5


引用 3 楼 schlafenhamster 的回复:
("\\.\n:") 这个 n 就是 'n' 而不是变量 CString n
应该怎么写

#6


引用 2 楼 oyljerry 的回复:
你要把变量n用CString的Format组合字符串。
应该怎么写呢 ,蟹蟹

#7


CString disk="D";

CString n;
n = "\\.\\" + disk;//[drive]; 
n +=":";

#8


引用 7 楼 schlafenhamster 的回复:
CString disk="D";

CString n;
n = "\\.\\" + disk;//[drive]; 
n +=":";

如果我获取到的盘符不是D怎么办,A B C 呢

#9


引用 8 楼 nanluming7891 的回复:
Quote: 引用 7 楼 schlafenhamster 的回复:

CString disk="D";

CString n;
n = "\\.\\" + disk;//[drive]; 
n +=":";

如果我获取到的盘符不是D怎么办,A B C 呢

可以帮忙解释解释么

#10


CStringArray disk;
disk.Addstring("A");
disk.Addstring("B");
disk.Addstring("C");
disk.Addstring("D");
 
CString n;
int drive=3;
 n = "\\.\\" + disk[drive]; // D
 n +=":";

#11


引用 2 楼 oyljerry 的回复:
你要把变量n用CString的Format组合字符串。
版主大大求解释

#12


引用 2 楼 oyljerry 的回复:
你要把变量n用CString的Format组合字符串。
我只需要知道怎么把 disk数组中drive这个内容(盘符)赋给变量,并且在CreateFile中有效就可以了

#13


我明白了,太傻了
CString *n= & disk[drive];
so 不就可以了嘛   对不对

#14


可以 但 
disk.Addstring("A");
要改为
disk.Addstring("\\.\\A:");
......

#15


要改为
disk.Addstring("\\\\.\\A:");
吧?

#16


格式化一下就好了~~

#17


本来的格式是要求 "\\.\C:"   格式化时 _T("\\\\.\\C:")

#18


C++ Character Constants
Character constants are one or more members of the “source character set,” the character set in which a program is written, surrounded by single quotation marks ('). They are used to represent characters in the “execution character set,” the character set on the machine where the program executes.

Microsoft Specific 

For Microsoft C++, the source and execution character sets are both ASCII.

END Microsoft Specific

There are three kinds of character constants: 

Normal character constants


Multicharacter constants


Wide-character constants 
Note   Use wide-character constants in place of multicharacter constants to ensure portability.

Character constants are specified as one or more characters enclosed in single quotation marks. For example:

char ch = 'x';          // Specify normal character constant.
int mbch = 'ab';        // Specify system-dependent
                        //  multicharacter constant.
wchar_t wcch = L'ab';   // Specify wide-character constant.

Note that mbch is of type int. If it were declared as type char, the second byte would not be retained. A multicharacter constant has four meaningful characters; specifying more than four generates an error message.

Syntax

character-constant :

'c-char-sequence'
L'c-char-sequence'

c-char-sequence :

c-char
c-char-sequence c-char

c-char :

any member of the source character set except the single quotation mark ('), backslash (\), or newline character
escape-sequence

escape-sequence :

simple-escape-sequence
octal-escape-sequence
hexadecimal-escape-sequence

simple-escape-sequence : one of

\' \" \? \\
\a \b \f \n \r \t \v

octal-escape-sequence :

\octal-digit
\octal-digit octal-digit
\octal-digit octal-digit octal-digit

hexadecimal-escape-sequence :

\xhexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit

Microsoft C++ supports normal, multicharacter, and wide-character constants. Use wide-character constants to specify members of the extended execution character set (for example, to support an international application). Normal character constants have type char, multicharacter constants have type int, and wide-character constants have type wchar_t. (The type wchar_t is defined in the standard include files STDDEF.H, STDLIB.H, and STRING.H. The wide-character functions, however, are prototyped only in STDLIB.H.)

The only difference in specification between normal and wide-character constants is that wide-character constants are preceded by the letter L. For example:

char schar = 'x';               // Normal character constant
wchar_t wchar = L'\x81\x19';    // Wide-character constant

Table 1.2 shows reserved or nongraphic characters that are system dependent or not allowed within character constants. These characters should be represented with escape sequences.

Table 1.2   C++ Reserved or Nongraphic Characters

Character ASCII 
Representation ASCII 
Value Escape Sequence 
Newline NL (LF) 10 or 0x0a \n 
Horizontal tab HT 9 \t 
Vertical tab VT 11 or 0x0b \v 
Backspace BS 8 \b 
Carriage return CR 13 or 0x0d \r 
Formfeed FF 12 or 0x0c \f 
Alert BEL 7 \a 
Backslash \ 92 or 0x5c \\ 
Question mark ? 63 or 0x3f \? 
Single quotation mark ' 39 or 0x27 \' 
Double quotation mark " 34 or 0x22 \" 
Octal number ooo — \ooo 
Hexadecimal number hhh — \xhhh 
Null character NUL 0 \0 


If the character following the backslash does not specify a legal escape sequence, the result is implementation defined. In Microsoft C++, the character following the backslash is taken literally, as though the escape were not present, and a level 1 warning (“unrecognized character escape sequence”) is issued.

Octal escape sequences, specified in the form \ooo, consist of a backslash and one, two, or three octal characters. Hexadecimal escape sequences, specified in the form \xhhh, consist of the characters \x followed by a sequence of hexadecimal digits. Unlike octal escape constants, there is no limit on the number of hexadecimal digits in an escape sequence.

Octal escape sequences are terminated by the first character that is not an octal digit, or when three characters are seen. For example:

wchar_t och = L'\076a';  // Sequence terminates at a
char    ch = '\233';     // Sequence terminates after 3 characters

Similarly, hexadecimal escape sequences terminate at the first character that is not a hexadecimal digit. Because hexadecimal digits include the letters a through f (and A through F), make sure the escape sequence terminates at the intended digit.

Because the single quotation mark (') encloses character constants, use the escape sequence \' to represent enclosed single quotation marks. The double quotation mark (") can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a character constant, you must type two backslashes in a row (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.)

#19


把 _T("\\.\n:") 的结果打印出来,看下是否正常,如果这就是你的实际写法,那肯定是不对的,
TCHAR part[16 = {0};
_stprintf(part, _T("\\\\.\\%c:"), n);

#20


  • CreateFile 打开 I/O device
  • 主机与I/O设备的信息交换方式

推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 关于我们EMQ是一家全球领先的开源物联网基础设施软件供应商,服务新产业周期的IoT&5G、边缘计算与云计算市场,交付全球领先的开源物联网消息服务器和流处理数据 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了在Pygame中使用矩形对表面进行涂色的方法。通过查阅Pygame文档中的blit函数,可以了解到如何将一个表面的特定部分复制到另一个表面的指定位置上。具体的解决方法和参数说明在文中都有详细说明。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了[从头学数学]中第101节关于比例的相关问题的研究和修炼过程。主要内容包括[机器小伟]和[工程师阿伟]一起研究比例的相关问题,并给出了一个求比例的函数scale的实现。 ... [详细]
author-avatar
songbird1471
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有