原文地址:
http://support.microsoft.com/kb/268901。
以下爲轉載文字:
可以使用以下步驟來創建 Win32 控制檯項目,將轉換爲行終止向"LF"包含"CR/LF"的文件:
- 使用 Win32 控制檯應用程序應用程序嚮導來創建名爲DOS2UNIX的新的空項目
- 從文件菜單上,單擊新建,然後單擊文件選項卡。
- 選擇C/c + + 源文件並命名新文件DOS2UNIX.cpp
- 將以下代碼粘貼到 DOS2UNIX.cpp:
#include<windows.h> #include<iostream> #include<fstream> using namespace std; int main(int argc, char* argv[]) { if(argc !=2) { cout << "Please specify : dos2unix filename" << endl; return 0; } char ch; char temp[MAX_PATH]="\0"; //Open the file for reading in binarymode. ifstream fp_read(argv[1], ios_base::in \ | ios_base::binary); sprintf(temp, "%s.temp", argv[1]); //Create a temporary file for writing in the binary mode. This //file will be created in the same directory as the input file. ofstream fp_write(temp, ios_base::out \ | ios_base::trunc \ | ios_base::binary); while(fp_read.eof() != true) { fp_read.get(ch); //Check for CR (carriage return) if((int)ch == 0x0D) continue; if (!fp_read.eof())fp_write.put(ch); } fp_read.close(); fp_write.close(); //Delete the existing input file. remove(argv[1]); //Rename the temporary file to the input file. rename(temp, argv[1]); //Delete the temporary file. remove(temp); return 0; }
- 從生成菜單中,單擊生成 DOS2UNIX.exe以生成的.exe 文件。
世界
可用於此 Visual C++ 自動化結合自動化整個過程。可以編寫一個簡單的 Microsoft Visual Basic 腳本宏調用此工具,但該工具必須先添加到工具菜單,如下所示:
- 從工具菜單上,單擊自定義,然後單擊工具選項卡。
- 指定一個名稱,如DOS2UNIX,並且提供的命令編輯框中的 Dos2unix.exe 文件的完整路徑。
- 對於參數,指定$(FileName)$(FileExt)
- 對於初始目錄,指定$(WkspDir) (指定您自己的路徑)。
如果您希望自動執行此過程,以便每次在 Visual C++ 編輯器中保存打開的文件,稱爲 DOS2UNIX.exe 工具去0x0Ds,然後使用下面的 VBScript 宏:
'This event is fired every time the document is saved in the VC++ Editor. Sub Application_DocumentSave(theDocument) 'This will call the user tool in the Tools menu. 'Change the number depending upon what you have. By default you only 'have 6 tools under the Tools menu, so the DOS2UNIX tool will be the 7th. ExecuteCommand "UserTool7" End Sub
只有具有 Visual C++ 在編輯器中打開一個文件,此 VBScript 代碼將起作用。這是從 VBScript 宏中 (不能向一個 VBScript 宏傳遞參數) 調用.exe 文件的唯一方法。相反,編寫外接程序,這將更爲靈活。而無需將其添加到工具菜單,您可以從外接程序中調用 DOS2UNIX.exe 工具。
若要在 Visual C++ 中使用提供的 VBScript 宏:
- 打開擴展名爲.dsm 的現有文件或創建一個。
- 將代碼粘貼到先前提供的文件中。
- Visual C++ 中執行下列操作:
- 從工具菜單上,單擊自定義
- 單擊外接程序和宏文件選項卡。
- 單擊瀏覽以加載包含該宏的.dsm 文件。一旦在瀏覽對話框中選擇.dsm 文件,您的文件將出現在外接程序和宏文件列表與選中其旁邊的複選框。
- 單擊關閉以繼續。