刚学VC请大家关照问题一:
怎么从服务器上下载文件到指定目录
比如:从http://zi.csdn.net/netease_2.gif文件下载到本机C:\download的文件夹问题二:
例如下面文件地读取(里面有回车等等,不晓得如何处理)
如何读取12
如何读取http://localhost/gf/abc.txt及后面地文件update
VERSION=12
FILE0=http://localhost/gf/abc.txt
FILE1=http://localhost/gf/ab.jpg
...........................
.........
怎么从服务器上下载文件到指定目录
比如:从http://zi.csdn.net/netease_2.gif文件下载到本机C:\download的文件夹问题二:
例如下面文件地读取(里面有回车等等,不晓得如何处理)
如何读取12
如何读取http://localhost/gf/abc.txt及后面地文件update
VERSION=12
FILE0=http://localhost/gf/abc.txt
FILE1=http://localhost/gf/ab.jpg
...........................
.........
用CStdioFile类打开文件,ReadString函数可以逐行读取。然后用=号拆分即可楼上地啊,能否说得详细点,我试这个了,老是错误,如何判断呢?并且到末尾
if (sf)
{
CString strLine;
while (sf.ReadString(strLine))
{
int nEq = strLine.Find(_T("="));
if (nEq != -1)
{
CString strVarName = strLine.Left(nEq);
CString strVarValue = strLine.Right(strLine.GetLength() - nEq - 1);
UINT uVersion = 0;
if (strVarName.CompareNoCase(_T("Version")) == 0)
{
nVersion = _tstol(strValue);
//对于版本处理
}
else if (strVarName.Left(4).CompareNoCase(_T("File")) == 0)
{
int nFileName = strValue.ReverseFind(_T("/"));
if (nFileName != -1)
{
CString strFileName = strValue.Right(strValue.GetLength() - nFileName - 1);
CString strFilePath = _T("C:\\Download\\") + strFileName;
HRESULT hr = URLDownloadToFile(NULL, strValue, strFilePath, 0, NULL);//这个下载可以放到线程中
if (SUCCEEDED(hr))
{
CString strMsg;
strMsg.Format(_T("文件%s已经下载为%s"), strValue, strFilePath);
AfxMessageBox(strMsg);
}
}
}
}
}
}没有调试
URLDownloadToFile
CStdioFile sf(_T("C:\\update.txt"), CFile::modeRead | CFile::typeText);
if (sf)
{
CString strLine;
while (sf.ReadString(strLine))
{
int nEq = strLine.Find(_T("="));
if (nEq != -1)
{
CString strVarName = strLine.Left(nEq);
CString strVarValue = strLine.Right(strLine.GetLength() - nEq - 1);
UINT uVersion = 0;
if (strVarName.CompareNoCase(_T("Version")) == 0)
{
uVersion = _tstol(strVarValue);
//对于版本处理
}
else if (strVarName.Left(4).CompareNoCase(_T("File")) == 0)
{
int nFileName = strVarValue.ReverseFind('/');
if (nFileName != -1)
{
CString strFileName = strVarValue.Right(strVarValue.GetLength() - nFileName - 1);
CString strFilePath = _T("C:\\Download\\") + strFileName;
HRESULT hr = URLDownloadToFile(NULL, strVarValue, strFilePath, 0, NULL);//这个下载可以放到线程中
if (SUCCEEDED(hr))
{
CString strMsg;
strMsg.Format(_T("文件%s已经下载为%s"), strVarValue, strFilePath);
AfxMessageBox(strMsg);
}
}
}
}
}
}
这个更好,因为IE使用的就是这个.它具有缓存的功能.