CFile open的问题

永远的明日 2008-12-28 01:23:34

CFile中的open怎么用的啊···?
怎么打不开存在的文件??各位大大帮忙看看下面代码
void down_to_client(SOCKET sa)
{
int bytes;
char tem [259]="dir ";
long file_length;
int exist = 1;
char buf[4096];
char pathname[256];


memset(buf,0,sizeof(buf));
recv(sa,buf,BUF_SIZE,0);
memset(&pathname,0,sizeof(pathname));
strcpy(pathname,dir);
strcat(pathname,"\\");
strcat(pathname,buf);
CFile file;

exist=file.Open(pathname,CFile:: modeRead); //pathname="c:\\a.txt"时,返回值为0,c盘下有这个文件的,当执行完这步,a.txt变为空
//双击打开提示有程序在使用该文件···
if(!exist)
{
memset(buf,0,sizeof(buf));
DeleteFile(pathname);
ltoa(0, buf, 10);
send(sa,buf,BUF_SIZE,0);
return ;
}
file_length = file.GetLength();
memset(buf,0,sizeof(buf));
ltoa(file_length, buf, 10);
send(sa,buf,BUF_SIZE,0);
while(1)
{
bytes = file.Read(buf,BUF_SIZE);
if(bytes<=0)break;
send(sa,buf,bytes,0);

}


}
...全文
725 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeyi771 2009-04-23
  • 打赏
  • 举报
回复
受用!!
nineforever 2008-12-31
  • 打赏
  • 举报
回复
CFile默认打开是exclusive的,所以不能同时有两个CFile打开同一个文件

参见CFile构造函数的nOpenFlags参数的说明:

CFile::shareDenyNone Opens the file without denying other processes read or write access to the file. Create fails if the file has been opened in compatibility mode by any other process.

CFile::shareDenyRead Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.

CFile::shareDenyWrite Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.

CFile::shareExclusive Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.

CFile::shareCompat This flag is not available in 32-bit and 64-bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.
abheyong 2008-12-29
  • 打赏
  • 举报
回复
这你都不知道
永远的明日 2008-12-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Chiyer 的回复:]
memset(buf,0,sizeof(buf));
recv(sa,buf,BUF_SIZE,0);
memset(&pathname,0,sizeof(pathname));
strcpy(pathname,dir);
strcat(pathname,"\\");
strcat(pathname,buf);
CFile file;


你看一下 pathname是不是最后没有字符串结束符?
[/Quote]
应该没有,因为我把pathname直接用"c:\\a.txt"代替也不行··
同时读一文件,写另一个文件应该没问题的吧???
SearchLife 2008-12-28
  • 打赏
  • 举报
回复
mark
zhaohongbo83 2008-12-28
  • 打赏
  • 举报
回复
看一下 MSDN 吧
星羽 2008-12-28
  • 打赏
  • 举报
回复
memset(buf,0,sizeof(buf));
recv(sa,buf,BUF_SIZE,0);
memset(&pathname,0,sizeof(pathname));
strcpy(pathname,dir);
strcat(pathname,"\\");
strcat(pathname,buf);
CFile file;


你看一下 pathname是不是最后没有字符串结束符?
hityct1 2008-12-28
  • 打赏
  • 举报
回复
看msdn,更改一下打开方式:

CFile::modeCreate
Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.


CFile::modeNoTruncate
Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well


文件用完要Close
永远的明日 2008-12-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 majun01 的回复:]
CFile的Open()是不支持文本方式读写
http://c.chinaitlab.com/cc/basic/200810/766694.html
[/Quote]
可为什么下面的又行??在open之前没打开该文件·

int main()
{
CFile file;
int exist = 1;
char pathname[256];
memset(&pathname,0,sizeof(pathname));
strcpy(pathname,"c:\\a.txt");
exist=file.Open(pathname,CFile:: modeRead);//返回值为1
cout<<exist<<endl;
}
majun01 2008-12-28
  • 打赏
  • 举报
回复
CFile的Open()是不支持文本方式读写
http://c.chinaitlab.com/cc/basic/200810/766694.html

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧