我使用以下代码创建一个文件,但它始终失败,错误代码为123(路径语法无效).
奇怪的是:path_ok总是正常,但path_err总是以123失败.在失败之后,path_err指向的缓冲区被清除.
谁能对我有所了解?我检查了2个指针的内存,它们的内容似乎是相同的.
非常感谢.
WCHAR *pDumpFileName = ComposeDumpFileName();
WCHAR *path_ok = _T("d:\\myapp_Utopia_2010-11-15_04-22-05.dmp");
WCHAR *path_err = pDumpFileName;
::wprintf(pDumpFileName);
HANDLE hFile = ::CreateFileW( pDumpFileName, GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
Run Code Online (Sandbox Code Playgroud)
ComposeDumpFileName()函数是这样的:
WCHAR* ComposeDumpFileName(void)
{
// get the time
SYSTEMTIME sys_time;
::GetSystemTime(&sys_time);
// get the computer name
WCHAR computer_name[MAX_COMPUTERNAME_LENGTH + 1];
DWORD computer_name_len = ARRAYSIZE(computer_name);
::GetComputerNameW(computer_name, &computer_name_len);
// build the filename: APPNAME_COMPUTERNAME_DATE_TIME.DMP
WCHAR dump_file_path[MAX_PATH];
::swprintf_s(dump_file_path, ARRAYSIZE(dump_file_path),
_T("d:\\myapp_%s_%04u-%02u-%02u_%02u-%02u-%02u.dmp"),
computer_name, sys_time.wYear, sys_time.wMonth, sys_time.wDay,
sys_time.wHour, sys_time.wMinute, sys_time.wSecond); …Run Code Online (Sandbox Code Playgroud) 我目前正在用C++编写一些测试代码,这些代码使用PE文件来理解它的文件格式结构.我的项目设置为64位编译.在我的代码中,我打开%SystemRoot%\ system32\calc.exe并读取IMAGE_DOS_HEADER和IMAGE_NT_HEADERS结构.同时我使用十六进制编辑器插件在Notepad ++中打开了相同的calc.exe.我将我的代码读取的值与Notepad ++进行了比较,发现它们不同.我将calc.exe从System32复制到C:\ Temp\calc.exe,现在值匹配.
Notepad ++似乎是一个32位应用程序(尚未检查PE文件,但由于它默认安装到Program Files(x86),所以它似乎是一个安全的假设).
这个WinSxS在起作用吗?或者还有什么导致这个?哪个文件实际上被送到32位应用程序打开%SystemRoot%\ system32\calc.exe?
只是好奇.在此先感谢任何光线.
大家,我每秒有很多文件写入磁盘,我想禁用磁盘缓存以提高性能,我谷歌搜索找到解决方案:win32 CreateFile method with FILE_FLAG_NO_BUFFERING 和 How toempty /flush Windows READ disk cache in C#? 。
我写了一些代码来测试是否可以工作:
const int FILE_FLAG_NO_BUFFERING = unchecked((int)0x20000000);
[DllImport("KERNEL32", SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)]
static extern SafeFileHandle CreateFile(
String fileName,
int desiredAccess,
System.IO.FileShare shareMode,
IntPtr securityAttrs,
System.IO.FileMode creationDisposition,
int flagsAndAttributes,
IntPtr templateFile);
static void Main(string[] args)
{
var handler = CreateFile(@"d:\temp.bin", (int)FileAccess.Write, FileShare.None,IntPtr.Zero, FileMode.Create, FILE_FLAG_NO_BUFFERING, IntPtr.Zero);
var stream = new FileStream(handler, FileAccess.Write, BlockSize);//BlockSize=4096
byte[] array = Encoding.UTF8.GetBytes("hello,world");
stream.Write(array, 0, array.Length);
stream.Close();
} …Run Code Online (Sandbox Code Playgroud) 某些Unicode字符(如)的代码点消耗超过2个字节.如何使用CreateFile()与这些字符一样的Win32 API函数?
WINBASE.H
WINBASEAPI
__out
HANDLE
WINAPI
CreateFileA(
__in LPCSTR lpFileName,
__in DWORD dwDesiredAccess,
__in DWORD dwShareMode,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__in DWORD dwCreationDisposition,
__in DWORD dwFlagsAndAttributes,
__in_opt HANDLE hTemplateFile
);
WINBASEAPI
__out
HANDLE
WINAPI
CreateFileW(
__in LPCWSTR lpFileName,
__in DWORD dwDesiredAccess,
__in DWORD dwShareMode,
__in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
__in DWORD dwCreationDisposition,
__in DWORD dwFlagsAndAttributes,
__in_opt HANDLE hTemplateFile
);
#ifdef UNICODE
#define CreateFile CreateFileW
#else
#define CreateFile CreateFileA
#endif // !UNICODE
Run Code Online (Sandbox Code Playgroud)
LPCSTR和LPCWSTR在WinNT.h中定义为:
typedef __nullterminated CONST CHAR *LPCSTR, *PCSTR; …Run Code Online (Sandbox Code Playgroud) 我正在尝试打开一个文本文件,将一些数据写入其中,然后在已经写入文件的数据末尾追加一些数据,但这不起作用.任何人都可以帮我解决我的代码问题吗? 代码链:
char buffer[]="Write this text to file";
DWORD dwWritten; // number of bytes written to file
HANDLE hFile;
hFile=CreateFile("file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(0,"Could not create/open a file","Error",16);
return 0;
}
WriteFile(hFile,buffer,sizeof(buffer),&dwWritten,0);
DWORD dwPtr = SetFilePointer( hFile, dwWritten, NULL, FILE_END); //set pointer position to end file
WriteFile(hFile,buffer,sizeof(buffer),&dwPtr,NULL);
CloseHandle(hFile);
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的应用程序的每次执行编写一个简单的日志文件.日志文件名基于当前时间并存储在app目录中.下面的代码是我正在尝试在模拟器上运行的代码,但是当我在iPad上执行时它失败并且"不允许操作"(errno.h中的EPERM).
-(BOOL)CreateLogFile
{
mLogFilePath = [self GetLogFileNameForTimeNow];
BOOL Success = [[NSFileManager defaultManager] createFileAtPath:mLogFilePath contents:nil attributes:nil];
/*BOOL Success = [[NSFileManager defaultManager] createFileAtPath:mLogFilePath contents:nil attributes:[NSDictionary dictionaryWithObject:NSFileProtectionComplete
forKey:NSFileProtectionKey]];*/
if ( Success )
{
NSLog( @"Successfully created file: %@", mLogFilePath );
mLogFileHandle = [NSFileHandle fileHandleForWritingAtPath:mLogFilePath];
[self WriteInstanceInformationToLog];
}
else
{
NSLog( @"Failed to create file: %@ %s", mLogFilePath, strerror(errno) );
}
return Success;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试了注释掉的代码,但也失败了.我是iOS编程的新手,所以可能会遗漏一些基本的东西.无论如何,上面的输出形式在iPad上显示如下:
2013-05-10 14:54:51.794 RichTest1[128:907] Failed to create file:
/var/mobile/Applications/XXXX/RichTest1.app/20130510145451.log Operation not permitted
Run Code Online (Sandbox Code Playgroud)
但在模拟器上它的工作原理:
2013-05-10 15:07:14.696 RichTest1[1604:c07] Successfully created file:
Users/abruce/Library/Application …Run Code Online (Sandbox Code Playgroud) 我是python的新手。
我想知道是否可以在写入之前在脚本本身中创建一个文本文件。我不想使用命令提示符创建文本文件。
我写了这个脚本来将结果写入文件
with open('1.txt', 'r') as flp:
data = flp.readlines()
Run Code Online (Sandbox Code Playgroud)
但是我知道在写入之前必须创建 1.txt。
任何帮助将不胜感激。
我需要同时(即异步)从文件中读取几行。文件中的行大小相同。
例如,我需要读取文件的第二行和第四行以分隔变量或数组。
我更习惯于c#的异步/等待,所有这些OVERLAPPED事情对我来说都有点难以理解。
使用msdn示例,我实现了这一点,它只是将文件中的数据作为一个字符串读取(甚至是异步读取吗?):
BOOL ReadFromFileAsync(PCTSTR path)
{
HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
_tprintf_s(TEXT("INVALID_HANDLE_VALUE\n"));
return FALSE;
}
BOOL bResult;
BYTE bReadBuf[2048];
OVERLAPPED oRead = { 0 };
oRead.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
bResult = ReadFile(hFile, bReadBuf, _countof(bReadBuf), NULL, &oRead);
if (!bResult && GetLastError() != ERROR_IO_PENDING)
{
_tprintf_s(TEXT("ERROR io pending"));
CloseHandle(hFile);
return FALSE;
}
// perform some stuff asynchronously
_tprintf_s(TEXT("HEY\n"));
HANDLE hEvents[2];
hEvents[0] = oRead.hEvent;
hEvents[1] …Run Code Online (Sandbox Code Playgroud) 我想真正了解何时ERROR_USER_MAPPED_FILE发生。所以我写了一些片段。重现错误。但它没有用。请帮我修复我的代码
流程一:
HANDLE hFile = CreateFile("C:\\test\\full.exe", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return GetLastError();
HANDLE hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (hMapFile == INVALID_HANDLE_VALUE)
return GetLastError();
mapHandles.push_back(hMapFile);
viewHandles.push_back(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0));
Run Code Online (Sandbox Code Playgroud)
我正在暂停该进程,并且我尚未在此进程中关闭任何地图和文件句柄视图,以便在其他进程尝试打开文件时。我以为会抛出错误 1224。
过程2:
HANDLE hFile = CreateFile("C:\\test\\full.exe", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
cout << "Error Code : " << GetLastError() << endl;
char buffer[1025];
DWORD bytesRead; …Run Code Online (Sandbox Code Playgroud) 有没有办法FILE*根据WinAPI CreateFile在C++中返回的句柄创建stdio的结构?
createfile ×10
winapi ×6
c++ ×3
c ×2
windows ×2
c# ×1
file ×1
filenames ×1
filestream ×1
fopen ×1
ios ×1
objective-c ×1
pinvoke ×1
python ×1
string ×1
text-files ×1
unicode ×1
winsxs ×1
wow64 ×1