我的小程序是这样的:1.在main里面CreateFile打开一个文件
2.启动一个新的线程来Write一个字符串helloworld
3.在main里面等待线程完成,把内容读出来。但是Write的内容好像没有被写入硬盘,程序打印Read的内容为空。程序结束以后也没有磁盘文件生成。这到底是为什么呢? 我要求程序不要多次CreateFile/OpenFile,只打开一次,不同的线程执行读/写操作。
我应该如何修改? 请指点。程序代码如下:#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
HANDLE hFile;
void* fun(void*){
char hw[]="helloworld";
DWORD dwWriten;
if(!WriteFile(hFile,(LPVOID)hw,sizeof(hw),&dwWriten,NULL)){//ERROR_ACCESS_DENIED(5)
printf("WriteFile failed:%d\n",GetLastError());
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
hFile=CreateFile("d:\\my.txt",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_HIDDEN,
NULL);
if(INVALID_HANDLE_VALUE==hFile){
printf("CreateFile failed:%d\n",GetLastError());
return 1;
}
HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)fun,NULL,0,NULL);
if(NULL==hThread){
printf("CreateThread failed:%d\n",GetLastError());
return 1;
}
WaitForSingleObject(hThread,INFINITE);
char buf[20]={0};
DWORD dwRead;
if(!ReadFile(hFile,(LPVOID)buf,20,&dwRead,NULL)){
printf("ReadFile failed:%d\n",GetLastError());
return 1;
}
printf("content read:%s\n",buf);
CloseHandle(hFile);
return 0;
}
2.启动一个新的线程来Write一个字符串helloworld
3.在main里面等待线程完成,把内容读出来。但是Write的内容好像没有被写入硬盘,程序打印Read的内容为空。程序结束以后也没有磁盘文件生成。这到底是为什么呢? 我要求程序不要多次CreateFile/OpenFile,只打开一次,不同的线程执行读/写操作。
我应该如何修改? 请指点。程序代码如下:#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
HANDLE hFile;
void* fun(void*){
char hw[]="helloworld";
DWORD dwWriten;
if(!WriteFile(hFile,(LPVOID)hw,sizeof(hw),&dwWriten,NULL)){//ERROR_ACCESS_DENIED(5)
printf("WriteFile failed:%d\n",GetLastError());
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
hFile=CreateFile("d:\\my.txt",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_HIDDEN,
NULL);
if(INVALID_HANDLE_VALUE==hFile){
printf("CreateFile failed:%d\n",GetLastError());
return 1;
}
HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)fun,NULL,0,NULL);
if(NULL==hThread){
printf("CreateThread failed:%d\n",GetLastError());
return 1;
}
WaitForSingleObject(hThread,INFINITE);
char buf[20]={0};
DWORD dwRead;
if(!ReadFile(hFile,(LPVOID)buf,20,&dwRead,NULL)){
printf("ReadFile failed:%d\n",GetLastError());
return 1;
}
printf("content read:%s\n",buf);
CloseHandle(hFile);
return 0;
}
解决方案 »
- 帮我看一下,为什么一到Accept那里就失败?
- 大家在处理IOCP连接的时候,就是发送PostAccept的时候是发送连接还是连接和数据一起?
- 怎样判断一个窗口是否是一个按钮???
- 靶点问题!!新手求助
- CSplitterWnd的全屏处理
- ODBC数据库开发:动态打开表格
- 请教,在mfc程序中使用COleInsertDialog插入了excel,如何获得插入的excel对象,比如获得(1,1)的文本?
- 怎样把普通的dll包装为atl com
- 高分求解(贴子加200分),一定给分!!!(急!!!工作之需)
- 急!求救!!!
- sendto 后错误值getlasterror为10038
- 调用ShBrowserFolder,但是不查询它的IMalloc接口来释放字符串的内存,会有内存泄漏吗?
#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
HANDLE hFile;
void* fun(void*){
TCHAR hw[]= _T("helloworld");
DWORD dwWriten;
if(!WriteFile(hFile,(LPVOID)hw,sizeof(hw),&dwWriten,NULL)){//ERROR_ACCESS_DENIED(5)
printf("WriteFile failed:%d\n",GetLastError());
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
hFile=CreateFile(_T("F:\\my.txt"),
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_HIDDEN,
NULL);
if(INVALID_HANDLE_VALUE==hFile){
printf("CreateFile failed:%d\n",GetLastError());
return 1;
}
HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)fun,NULL,0,NULL);
if(NULL==hThread){
printf("CreateThread failed:%d\n",GetLastError());
return 1;
}
WaitForSingleObject(hThread,INFINITE);
CloseHandle(hFile); TCHAR buf[20]={0};
DWORD dwRead;
hFile=CreateFile(_T("F:\\my.txt"),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_HIDDEN,
NULL);
if(!ReadFile(hFile,(LPVOID)buf,sizeof(buf),&dwRead,NULL)){
printf("ReadFile failed:%d\n",GetLastError());
return 1;
}
_tprintf(_T("content read:%s\n"),buf);
CloseHandle(hFile);
return 0;
}
大虾,可不可以,不需要两次CreateFile,CloseHandle,就完成我的需求呢?
#include<windows.h>
#include<stdio.h>
HANDLE hFile;
void* fun(void*){
char hw[]="helloworld";
DWORD dwWriten;
if(!WriteFile(hFile,(LPVOID)hw,sizeof(hw),&dwWriten,NULL)){//ERROR_ACCESS_DENIED(5)
printf("WriteFile failed:%d\n",GetLastError());
}
if(!FlushFileBuffers(hFile)){
printf("Flush failed\n");
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
hFile=CreateFile("d:\\my.txt",
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(INVALID_HANDLE_VALUE==hFile){
printf("CreateFile failed:%d\n",GetLastError());
return 1;
}
HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)fun,NULL,0,NULL);
if(NULL==hThread){
printf("CreateThread failed:%d\n",GetLastError());
return 1;
}
WaitForSingleObject(hThread,INFINITE);
char buf[20]={0};
DWORD dwRead;
if(!ReadFile(hFile,(LPVOID)buf,20,&dwRead,NULL)){
printf("ReadFile failed:%d\n",GetLastError());
return 1;
}
printf("content read:%s\n",buf);
CloseHandle(hFile);
return 0;
}
#include "stdafx.h"#include<windows.h>
#include<stdio.h>
HANDLE hFile;
void* fun(void*){
char hw[]="helloworld";
DWORD dwWriten;
if(!WriteFile(hFile,(LPVOID)hw,sizeof(hw),&dwWriten,NULL)){//ERROR_ACCESS_DENIED(5)
printf("WriteFile failed:%d\n",GetLastError());
}
if(!FlushFileBuffers(hFile)){
printf("Flush failed\n");
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
hFile=CreateFile(_T("F:\\my.txt"),
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_WRITE|FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(INVALID_HANDLE_VALUE==hFile){
printf("CreateFile failed:%d\n",GetLastError());
return 1;
}
HANDLE hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)fun,NULL,0,NULL);
if(NULL==hThread){
printf("CreateThread failed:%d\n",GetLastError());
return 1;
}
WaitForSingleObject(hThread,INFINITE); SetFilePointer(hFile, 0, 0, FILE_BEGIN); char buf[20]={0};
DWORD dwRead;
if(!ReadFile(hFile,(LPVOID)buf,20,&dwRead,NULL)){
printf("ReadFile failed:%d\n",GetLastError());
return 1;
}
printf("content read:%s\n",buf);
CloseHandle(hFile);
return 0;
}
后面加上这句
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
Bingo,这个就能搞定!谢谢!