This article describes a procedure for saving an edited file created on a Windows platform in a format that can be used on UNIX systems.
NOTE: The Visual C++ .NET IDE has a feature available to save a file in UNIX format. In IDE, save the file using Save As..., from the Save drop-down list, select Save with Encoding..., and thrn click Yes. From the Line Encoding drop-down list, select Unix (LF), and then click OK.
- Use the Win32 Console Application AppWizard to create a new empty project named DOS2UNIX.
- From the File menu, click New, and then click the Files tab.
- Select C/C++ Source File and name the new file
- Paste the following code into DOS2UNIX.cpp:
using namespace std;
int main(int argc, char* argv)
cout << "Please specify : dos2unix filename" << endl;
//Open the file for reading in binarymode.
ifstream fp_read(argv, ios_base::in \
sprintf(temp, "%s.temp", argv);
//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 \
while(fp_read.eof() != true)
//Check for CR (carriage return)
if((int)ch == 0x0D)
//Delete the existing input file.
//Rename the temporary file to the input file.
//Delete the temporary file.
- From the Build menu, click Build DOS2UNIX.exe to generate the .exe file.
You can use this in conjunction with Visual C++ automation to automate the entire process. A simple Microsoft Visual Basic Script macro can be written to call this tool, but first this tool must be added to the Tools menu as follows:
- From the Tools menu, click Customize, and then click the Tools tab.
- Specify a name, such as DOS2UNIX, and provide the full path to the Dos2unix.exe file in the Command edit box.
- For argument, specify
- For initial directory, specify
$(WkspDir) (specify your own path).
If you want to automate this process so that every time you save an opened file in the Visual C++ editor, the DOS2UNIX.exe tool is called to remove the 0x0Ds, then use the following VBScript macro:
'This event is fired every time the document is saved in the VC++ Editor.
'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.
This VBScript code will work only if you have a file open in the Visual C++ editor. This is the only way to call an .exe file from a VBScript macro (you cannot pass parameters to a VBScript macro). You can write an add-in instead, and this would be more flexible. You can call the DOS2UNIX.exe tool from an add-in without having to add it to the Tools menu.
To use the provided VBScript macro in Visual C++:
- Open an existing file that has a .dsm extension or create one.
- Paste the code provided previously into the file.
- In Visual C++ do the following:
- From the Tools menu, click Customize.
- Click the Add-ins and Macros Files tab.
- Click Browse to load the .dsm file that contains the macro. Once the .dsm file has been selected in the Browse dialog box, your file will appear in the Add-ins and macro file list with a selected checkbox next to it.
- Click Close to continue.
If you are using Microsoft Visual SourceSafe as the source code management tool, then see the following Knowledge Base article:
Artikelnummer: 268901 – Letzte Überarbeitung: 14.08.2008 – Revision: 1