Ⅰ 求标准C语言读写文件的源代码
C语言读写文件 都是三个步骤
1.获取文件描述符
2.对文件读写
3.关闭文件
对文件读操作
//获取文件指针
FILE*pFile=fopen("1.txt","w");//文件打开方式如果原来有内容也会销毁//向文件写数据
fwrite("hello",//要输入的文字
1,//文字每一项的大小以为这里是字符型的就设置为1如果是汉字就设置为4
strlog("hello"),//单元个数我们也可以直接写5
pFile//我们刚刚获得到的地址
);
//fclose(pFile);//告诉系统我们文件写完了数据更新,但是我们要要重新打开才能在写
fflush(pFile);//数据刷新数据立即更新
对文件写操作
FILE*pFile=fopen("1.txt","r");//获取文件的指针
char*pBuf;//定义文件指针
fseek(pFile,0,SEEK_END);//把指针移动到文件的结尾,获取文件长度
intlen=ftell(pFile);//获取文件长度
pBuf=newchar[len+1];//定义数组长度
rewind(pFile);//把指针移动到文件开头因为我们一开始把指针移动到结尾,如果不移动回来会出错
fread(pBuf,1,len,pFile);//读文件
pBuf[len]=0;//把读到的文件最后一位写为0要不然系统会一直寻找到0后才结束
MessageBox(pBuf);//显示读到的数据
fclose(pFile);//关闭文件
Ⅱ c++如何获取网页内容,要控制台程序就行;越简单越好;就范例输出百度的源码吧!谢了急用!
我有,是MFC工程,留邮箱我发给你
关键代码在这里
void CGetPublicIPDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGetPublicIPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CGetPublicIPDlg::GetFromWeb(LPSTR pURL, LPSTR SaveAsFilePath)
{
CInternetSession session; //会话期对象)
CHttpConnection* pServer = NULL; //指向服务器地址(URL)
CHttpFile * pHttpFile = NULL;
CString strServerName; //服务器名
CString strObject; //查询对象名(http文件)
INTERNET_PORT nPort; //端口
DWORD dwServiceType; //服务类型
DWORD dwHttpRequestFlags = //请求标志
//INTERNET_FLAG_EXISTING_CONNECT
INTERNET_FLAG_NO_AUTO_REDIRECT;
const TCHAR szHeaders[] = _T("Accept:text/*\r\nUser-Agent:Client\r\n");
BOOL OK=AfxParseURL( //词法分析
pURL, //被分析URL串
dwServiceType, //服务类型,ftp,http等
strServerName, //服务器名
strObject, //URL中被查询对象
nPort ); //URL指定的端口,可能为空
OK=OK && //本例只考虑http协议
(dwServiceType ==
INTERNET_SERVICE_HTTP);
if (!OK)
{ AfxMessageBox("URL出错"); //报错
return false;
}
pServer = session.GetHttpConnection(strServerName, nPort); //获得服务器名
pHttpFile = pServer-> OpenRequest( CHttpConnection::HTTP_VERB_GET,strObject, NULL, 1, NULL, NULL,dwHttpRequestFlags);
//向服务器发送请求,建立http连接,
//建立本机上的http文件指针
pHttpFile->AddRequestHeaders(szHeaders);
pHttpFile->SendRequest(); //发送请求
CStdioFile f; //输出文件对象
if( !f.Open( //打开输出文件
SaveAsFilePath, CFile::modeCreate | CFile::modeReadWrite | CFile::typeText ) )
{
MessageBox( "Unable to open file");
return false;
}
//下面将检索结果保存到文件上
TCHAR szBuf[1024]; //缓存
while (pHttpFile->ReadString(szBuf, 1023))
f.WriteString( szBuf );
f.Close(); //善后工作
pHttpFile ->Close();
pServer ->Close();
if (pHttpFile != NULL) delete pHttpFile;
if (pServer != NULL) delete pServer;
session.Close();
/* while (f.ReadString( szBuf, 1023 ))
pHttpFile->WriteString(_T("123"));
f.Close(); //善后工作
pHttpFile ->Close();
pServer ->Close();
if (pHttpFile != NULL) delete pHttpFile;
if (pServer != NULL) delete pServer;
session.Close(); */
return true;
}
void CGetPublicIPDlg::OnOK()
{
// TODO: Add extra validation here
GetFromWeb("http://www..com","d:\\ip.txt");
CDialog::OnOK();
}