Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Editing Unix Files in Windows

1.39/5 (8 votes)
26 Jul 2006CPOL 1   201  
This is a very simple tool to edit Unix files in Windows.

Sample screenshot

Introduction

This tools allows you to edit files in Unix. vi is a good choice, but for Windows users, it is really a tough one. To simplify text editing, I have created a tool which actually downloads the file from a Unix server using FTP, and after editing it, uploads it back to the server.

Here are the files you need:

  • NetDirectoryDlg.cpp
  • FileEditor.cpp

It has only two dialogs:

  1. The NeDirectory dialog which captures the user ID, password, and server, and connects to the server and retrieves the file and directory information.
  2. Editor dialog which simply displays the file data.

This tool simply downloads the file from the server using FTP to a local machine. The local file is fed to the file editor. Once editing is done, it is uploaded back to the server using FTP.

I have used MFC's CFtpConnection for this.

The code

Connecting to the FTP server

C++
try
{
      if(inetsess==NULL)
      {
           inetsess=new CInternetSession();
      }
      else
      {
           inetsess->Close();
           inetsess=new CInternetSession();
      }
    ftp = inetsess->GetFtpConnection(m_server,m_user,m_pass);
}
catch(...)
{}
..
..
    GetFiles();
 }
}

Storing the records in the listbox

C++
CFtpFileFind ftpFind(ftp);
BOOL bcontinue=TRUE;
CString str;
/*search for records and update in list box*/
m_LstDir.ResetContent();
ftpFind.FindFile("");
while(bcontinue )
{
    bcontinue = ftpFind.FindNextFile();
      str = ftpFind.GetFileName();
      if(str.GetLength()>0)
      {
           if(ftpFind.IsDirectory())
           {
                str.Insert(0,"./");
           }
           m_LstDir.AddString(str);
      }
     }
     ftpFind.Close();
}

Double clicking the listbox and loading the file

C++
CString str;
CString strCurDir;
CFile file;
m_LstDir.GetText(m_LstDir.GetCurSel(),str);
if(str.Left(2)!="./")
{
    ftp->GetFile(str,str); /*get the file to local directory*/
    if(file.Open(str,CFile::modeRead))
    {
       file.Close();
       CFileEditor edit(str);
       if(edit.DoModal()==IDOK)
       {
            if(ftp->PutFile(str,str))
            {
                 AfxMessageBox("Server File Updated...",MB_OK);
            }
       }
       /*remove it in either case*/
       CFile::Remove(str);
    }
}
else
{
    if(ftp->GetCurrentDirectory(strCurDir))
    {
         strArrPath.Add(strCurDir);
    }
    if(ftp->SetCurrentDirectory(str))
    {
         GetFiles();
    }
}

It can be customized with features like a treeview control or a full Explorer like view.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)