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);

}


}
...全文
700 10 打赏 收藏 转发到动态 举报
写回复
用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
CFile //创建/打开文件 CFile file; file.Open(_T("test.txt"),CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite); 文件打开模式可组合使用,用“|”隔开,常用的有以下几种: CFile::modeCreate:以新建方式打开,如果文件不存在,新建;如果文件已存在,把该文件长度置零,即清除文件原有内容。 CFile::modeNoTruncate:以追加方式打开,如果文件存在,打开并且不将文件长度置零,如果文件不存在,会抛出异常。一般与CFile::modeCreate一起使用,则文件不存在时,新建一个文件;存在就进行追加操作。 CFile::modeReadWrite:以读写方式打开文件。 CFile::modeRead:只读。 CFile::modeWrite:只写。 //写入数据 CString strValue = "Hello World!"; file.Write(strValue,strValue.GetLength()); //追加数据 file.SeekToEnd(); //将指针移至文件末尾进行追加 file.Write(strValue,strValue.GetLength()); //关闭文件 file.Close(); CStdioFile CStdioFile是CFile的派生类,对文件进行流式操作,对于文本文件的读写很有用处,可按行读取写入。 //写入数据 CString strValue = "Hello World!"; file.WriteString(strValue); //读取数据 CString strRead; file.ReadString(strRead); 当文件存在多行数据需要逐行读取时,可用函数BOOL CStdioFile::ReadString(CString& rString),当遇到"\n "时读取截断,如果文件未读完,返回true,否则返回false。 //逐行读取文件内容,存入strRead while(file.ReadString(strRead)) { ...; } 各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的。本文将对Visual C++中有关文件操作进行全面的介绍,并对在文件操作中经常遇到的一些疑难问题进行详细的分析。   1.文件的查找   当对一个文件操作时,如果不知道该文件是否存在,就要首先进行查找。MFC中有一个专门用来进行文件查找的类CFileFind,使用它可以方便快捷地进行文件的查找。下面这段代码演示了这个类的最基本使用方法。   CString strFileTitle;   CFileFind finder;   BOOL bWorking = finder.FindFile("C:\\windows\\sysbkup\\*.cab");   while(bWorking)   {   bWorking=finder.FindNextFile();   strFileTitle=finder.GetFileTitle();   }   2.文件的打开/保存对话框   让用户选择文件进行打开和存储操作时,就要用到文件打开/保存对话框。MFC的类CFileDialog用于实现这种功能。使用CFileDialog声明一个对象时,第一个BOOL型参数用于指定文件的打开或保存,当为TRUE时将构造一个文件打开对话框,为FALSE时构造一个文件保存对话框。   在构造CFileDialog对象时,如果在参数中指定了OFN_ALLOWMULTISELECT风格,则在此对话框中可以进行多选操作。此时要重点注意为此CFileDialog对象的m_ofn.lpstrFile分配一块内存,用于存储多选操作所返回的所有文件路径名,如果不进行分配或分配的内存过小就会导致操作失败。下面这段程序演示了文件打开对话框的使用方法。   CFileDialog mFileDlg(TRUE,NULL,NULL,   OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,   "All Files (*.*)|*.*||",AfxGetMainWnd());   CString str(" ",10000);   mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);   str

64,654

社区成员

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

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