原文地址:
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 文件。
48 65 6C 6 C 6F 0 D 0A 57 6F 72 6 C 64
这相当于:
Hello
世界
在命令提示符下运行命令dos2unix.exe < 名称 >。接下来,在 Visual C++ 二进制编辑器中打开该文件。您将看到0x0Ds 被删除。只要不编辑该文件并将其保存在 Visual C++ 0x0Ds 不会出现。
世界
可用于此 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 文件,您的文件将出现在外接程序和宏文件列表与选中其旁边的复选框。
- 单击关闭以继续。