CreateFile
我用QT编译时为什么会报错呢
D:\project\Qt\123456\main.cpp:14: 错误:cannot convert 'const char*' to 'const WCHAR*' for argument '1' to 'void* CreateFileW(const WCHAR*, DWORD, DWORD, _SECURITY_ATTRIBUTES*, DWORD, DWORD, void*)'
hFile = CreateFile("5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
这样有错么?
关于CreateFile函数
答案:3 悬赏:10
解决时间 2021-03-18 15:15
- 提问者网友:爱你等于作孽
- 2021-03-17 16:15
最佳答案
- 二级知识专家网友:何必打扰
- 2021-03-17 16:39
不行的话你就强制写CreateFileA而不是CreateFile吧。
CreateFile有两个版本,CreateFileA和CreateFileW。
只写CreateFile的话它会根据工程的设置自动选择一个来用。
另外我赞成 _T 宏的方式,这也是让编译器自动选择一种字符串的表示方式来用
CreateFile有两个版本,CreateFileA和CreateFileW。
只写CreateFile的话它会根据工程的设置自动选择一个来用。
另外我赞成 _T 宏的方式,这也是让编译器自动选择一种字符串的表示方式来用
全部回答
- 1楼网友:末路丶一枝花
- 2021-03-17 19:00
楼主太不厚道,明明是我新回答的!
常量字符串默认是char*,你的代码需要强制类型转换,请用如下方法试试:
hFile = CreateFile(L"5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
或者
hFile = CreateFile(_T("5.txt"),GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
- 2楼网友:恕我颓废
- 2021-03-17 17:54
你找的是c++里的创建文件的函数,在c#里应该是用下面的
--------------------------------------------------------------------------------------
file 类
提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建 filestream 对象。
继承层次结构
system.object
system.io.file
命名空间: system.io
程序集: mscorlib(在 mscorlib.dll 中)
语法
c#
public static class file
file 类型公开以下成员。
公共方法静态成员 create(string) 在指定路径中创建或覆盖文件。
公共方法静态成员 create(string, int32) 创建或覆盖指定的文件。
公共方法静态成员 create(string, int32, fileoptions) 创建或覆盖指定的文件,并指定缓冲区大小和一个描述如何创建或覆盖该文件的 fileoptions 值。
公共方法静态成员 create(string, int32, fileoptions, filesecurity) 创建或覆盖具有指定的缓冲区大小、文件选项和文件安全性的指定文件。
下面的示例在指定路径中创建一个文件,将一些信息写入该文件,再从文件中读取。
using system;
using system.io;
using system.text;
class test
{
public static void main()
{
string path = @"c:\temp\mytest.txt";
try
{
// delete the file if it exists.
if (file.exists(path))
{
// note that no lock is put on the
// file and the possibility exists
// that another process could do
// something with it between
// the calls to exists and delete.
file.delete(path);
}
// create the file.
using (filestream fs = file.create(path))
{
byte[] info = new utf8encoding(true).getbytes("this is some text in the file.");
// add some information to the file.
fs.write(info, 0, info.length);
}
// open the stream and read it back.
using (streamreader sr = file.opentext(path))
{
string s = "";
while ((s = sr.readline()) != null)
{
console.writeline(s);
}
}
}
catch (exception ex)
{
console.writeline(ex.tostring());
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯