以下是我的程序:
try
{
//读取模版程序
CFile m_fileOpen,m_fileWrite;
CFileException m_fileE;
if (m_fileOpen.Open(_T(".\\模版\\redline.txt"),CFile::modeRead))
{
char cRead;
DWORD dwByteOfFile = (DWORD)m_fileOpen.GetLength();
UINT nByteOfRead;
m_fileWrite.Open(_T(".\\转换结果\\mytry.htm"),CFile::modeCreate|CFile::modeWrite,&m_fileE);
while (dwByteOfFile)
{
nByteOfRead = m_fileOpen.Read(&cRead,sizeof(cRead));
dwByteOfFile -= nByteOfRead;
m_fileWrite.Write(&cRead,nByteOfRead);
}
m_fileOpen.Close();
}
if (m_fileOpen.Open(_T(strFilePath[0]),CFile::modeRead,&m_fileE))
{
char cRead;
DWORD dwByteOfFile=(DWORD)m_fileOpen.GetLength();
UINT nByteOfRead;
//BYTE buffer[0x1000]; while (dwByteOfFile)
{
nByteOfRead = m_fileOpen.Read(&cRead,sizeof(cRead));
dwByteOfFile -= nByteOfRead;
m_fileWrite.Write(&cRead,nByteOfRead);
}
m_fileOpen.Close();
m_fileWrite.Close();
}
else
{
m_fileE.ReportError();
}
//GetDlgItem(IDC_FILEPATH)->GetWindowText(NULL,1);
}
catch (CException* e)
{
e->ReportError();
}
程序介绍:该程序的目的是合并两个文本文件.
问题:当我不指定文件二时,文件一的相对路径能够正常被解析,成功读写文件一,但一旦加入文件二后,结果文件一的相对路径就出错了,无法解析,调试发现文件一的路径总是被指定到当前文件所在盘符下的"\模版\redline.txt",例如:e:\模版\redline.txt,请问问题到底出在哪里?

解决方案 »

  1.   

    这种方法跟操作有关联,试试这种想法首先取出程序的路径,然后提取绝对路径,再加上文件名,形成唯一的路径比如a.txt,程序在d:\vc\test\debug下,那么和起来就是
    d:\vc\test\debug\a.txt程序的路径可以用__argv[ 0 ]来取得。
    CString
    GetMyPath( void )
    {
    CString s;
    char tt[ 512 ] = "";
    int i = strlen( __argv[ 0 ] );
    memcpy( tt, __argv[ 0 ], i ); while( tt[ i ] != '\\' )
    {
    i--;
    } s = ( CString )( tt + i + 1 );
    return s;
    }
      

  2.   

    通常,windows会记住每一个程序的文件打开目录,当使用相对路径时,便从上次的文件打开目录作为起始目录.
    bool GetAppPath(CString &path)//不包括最后的'\\'
    {
        CString sPath;
        GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
        sPath.ReleaseBuffer();
    int i=sPath.GetLength()-1;
    while(i>0)
    {
    if(sPath.GetAt(i)=='/' || sPath.GetAt(i)=='\\') break;
    i--;
    }
    if(i<0) return false;
    if(i==0) path=".";
    else path=sPath.Left(i);
    return true;
    }