`
runfeel
  • 浏览: 905236 次
文章分类
社区版块
存档分类
最新评论

C++文件操作的5种方式

 
阅读更多

纯C语言读取文件方式

写文件

FILE *pFile;
<wbr>pFile=fopen("jingge.txt","w");<br><wbr>fwrite("http://blog.sina.com.cn/liyuanjinglyj<a href="http://blog.sina.com.cn/liyuanjinglyj">",1,strlen("http://blog.sina.com.cn/liyuanjinglyj")+1,pFile</a>);<br><wbr>fseek(pFile,0,SEEK_SET);<br><wbr>fwrite("liyuanjing",1,strlen("liyuanjing"),pFile);<br><wbr>//fclose(pFile);<br><wbr>fflush(pFile);</wbr></wbr></wbr></wbr></wbr></wbr>

读文件

<wbr>FILE *pFile;<br><wbr>pFile=fopen("jingge.txt","r"); <p>char *pChr;<br><wbr>fseek(pFile,0,SEEK_END);<br><wbr>int len=ftell(pFile);<br><wbr>pChr=new char[len+1];<br><wbr>rewind(pFile);<br><wbr>fread(pChr,1,len,pFile);<br><wbr>fclose(pFile);<br><wbr>pChr[len]=0;<br><wbr>MessageBox(pChr);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>C++读写文件方式</p> <p>写文件</p> <p>ofstream ofs("4.txt");<br> ofs.write("http://blog.sina.com.cn/liyuanjinglyj<a href="http://blog.sina.com.cn/liyuanjinglyj">",strlen("http://blog.sina.com.cn/liyuanjinglyj</a>"));<br> ofs.close();</p> <p>读文件</p> <p>ifstream ifs("4.txt");<br><wbr>char ch[100];<br><wbr>memset(ch,0,100);<br><wbr>ifs.read(ch,100);<br><wbr>ifs.close();<br><wbr>MessageBox(ch);</wbr></wbr></wbr></wbr></wbr></p> <p><wbr></wbr></p> <p>Windows API读写文件方式</p> <p>写文件</p> <p>HANDLE pFile;<br><wbr>pFile=CreateFile("5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);<br><wbr>DWORD dwWrite;<br><wbr>WriteFile(pFile,"http://blog.sina.com.cn/liyuanjinglyj<a href="http://blog.sina.com.cn/liyuanjinglyj">",strlen("http://blog.sina.com.cn/liyuanjinglyj"),&amp;dwWrite,NULL</a>);<br><wbr>CloseHandle(pFile);</wbr></wbr></wbr></wbr></p> <p>读文件</p> HANDLE hFile;<br><wbr>hFile=CreateFile("5.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);<br><wbr>char ch[100];<br><wbr>DWORD dwRead;<br><wbr>ReadFile(hFile,ch,100,&amp;dwRead,NULL);<br><wbr>ch[dwRead]=0;<br><wbr>CloseHandle(hFile);<br><wbr>MessageBox(ch); <p><wbr></wbr></p> <p>CFile类读写文件方式</p> <p>写文件</p> <p>CFile file("6.txt",CFile::modeCreate | CFile::modeWrite);<br><wbr>file.Write("http://blog.sina.com.cn/liyuanjinglyj<a href="http://blog.sina.com.cn/liyuanjinglyj">",strlen("http://blog.sina.com.cn/liyuanjinglyj</a>"));<br><wbr>file.Close();</wbr></wbr></p> <p>读文件</p> <p>CFile file("6.txt",CFile::modeRead);<br><wbr>char *pChr;<br><wbr>DWORD dwFileLen;<br><wbr>dwFileLen=file.GetLength();<br><wbr>pChr=new char[dwFileLen+1];<br><wbr>pChr[dwFileLen]=0;<br><wbr>file.Read(pChr,dwFileLen);<br><wbr>file.Close();<br><wbr>MessageBox(pChr);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p><wbr></wbr></p> <p>MFC提供的CFileDialog方式读写文件</p> <p>写文件</p> <p>CFileDialog filedlg(FALSE);<br><wbr>filedlg.m_ofn.lpstrTitle="静哥另存为对话框";<br><wbr>filedlg.m_ofn.lpstrFilter="Text file(*.txt)\0*.txt\0ALL file(*.*)\0*.*\0\0";<br><wbr>filedlg.m_ofn.lpstrDefExt="txt";<br><wbr>if(IDOK==filedlg.DoModal())<br><wbr>{<br><wbr><wbr>CFile file(filedlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);<br><wbr><wbr>file.Write("http://blog.sina.com.cn/liyuanjinglyj<a href="http://blog.sina.com.cn/liyuanjinglyj">",strlen("http://blog.sina.com.cn/liyuanjinglyj</a>"));<br><wbr><wbr><wbr><wbr>file.Close();<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>读文件</p> <p>CFileDialog filedlg(TRUE);<br><wbr>filedlg.m_ofn.lpstrTitle="静哥另存为对话框";<br><wbr>filedlg.m_ofn.lpstrFilter="Text file(*.txt)\0*.txt\0ALL file(*.*)\0*.*\0\0";<br><wbr>if(IDOK==filedlg.DoModal())<br><wbr>{<br><wbr><wbr>CFile file(filedlg.GetFileName(),CFile::modeRead);<br><wbr><wbr>char *pChr;<br><wbr><wbr>DWORD dwFileLen;<br><wbr><wbr>dwFileLen=file.GetLength();<br><wbr><wbr>pChr=new char[dwFileLen+1];<br><wbr><wbr>pChr[dwFileLen]=0;<br><wbr><wbr>file.Read(pChr,dwFileLen);<br><wbr><wbr>file.Close();<br><wbr><wbr><wbr><wbr>MessageBox(pChr);<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics