Windows & UNIX file formats

Source: Internet
Author: User

 

(1) question proposal
We may have encountered the following confusions:
A. How do I view the binary format of a file or data stream (displayed in hexadecimal format )?
B. Why is the shell script edited in Windows not executable in UNIX? Why do C source files edited in Windows cannot be compiled in some GCC compilers?
C. Why does a text file contain ^ m when I open it in the VI editor? How can I remove it? Why do I use Notepad on Windows to open UNIX files without line breaks?

(2) analysis and solutions

A. How do I view the binary format of a file (displayed in hexadecimal format )?
Method 1: use Ctrl + H in utraedit to switch to the hexadecimal editing mode. ** Note **: This method is defective. It displays a single "Line Break" at the end of the line as "Carriage Return" + "line feed.

Method 2: Use the file or stream binary viewing tool fbin.

B. Why is the shell script edited in Windows not executable in UNIX? Why do C source files edited in Windows cannot be compiled in some GCC compilers?

Cause analysis: the Unix shell script cannot recognize the "Carriage Return" (I .e., Cr, '\ R'). When the Windows file format is changed, it always uses "Carriage Return" + "line feed ", as a result, the shell in UNIX cannot be properly interpreted. Solution: Delete the "Press ENTER" in Windows format.

Method 1: Use VI to open the source file and replace '\ r \ N' with' \ n'; ** disadvantage **: it is not suitable for Batch jobs with a large number of files.

Method 2: Use utraedit to convert files in Windows format to Unix format. File --> Conversion --> UNIX to DOS; ** disadvantage **: it is not suitable for Batch jobs with a large number of files.

Method 3: dos2unix commands in UNIX, such as $ dos2unix-K xx. c; ** disadvantage **: This method has a fatal defect that changes the original file attributes, such as the executable attributes and other attributes of an executable shell script, after conversion, all files will be lost. ** advantages **: Suitable for Batch jobs with a large number of common files.

Method 4: win2unix (Windows and Unix can be used). The function is similar to dos2unix, such as win2unx xx. c; ** advantages **: It overcomes all the disadvantages of dos2unix and can retain any attribute of the source file. It is also suitable for Batch jobs with a large number of files.

C. Why does a text file contain ^ m when I open it in the VI editor? Why do I use Notepad on Windows to open UNIX files without line breaks?

Cause Analysis: To solve this problem, you must first identify the differences between UNIX and Windows text files. As follows:
1) Windows text files on disks always wrap in the form of "Carriage Return" + "line feed.
2) UNIX text files on disks always use line breaks (LF, '\ n') instead of line breaks ". (UNIX rules: When a UNIX text file is saved to a disk, it always automatically converts the "Carriage Return" to "Carriage Return" and saves it, when output to the terminal, the terminal automatically converts the "Carriage Return" to the "Carriage Return" output)

** Conclusion 1 **: When a Unix-format file is opened in Windows notepad, the line feed cannot be displayed because '\ R' is not in the file, the result shows all the contents in the same row.

** Conclusion 2 **: tools such as utraedit automatically detect whether the file contains '\ R'. When' \ R' is missing at the end of the row, generally, it prompts you to convert the format from UNIX to Windows (I believe this prompt is displayed ).

** Conclusion 3 **: utraedit, VI, and other tools automatically save the file according to the original format. That is, if the file is opened in Windows format, it will save the file in Windows format (automatic conversion is not performed ); for example, if the file is opened in UNIX format, it will save the file in UNIX format (without automatic conversion ).

 

Http://down.51cto.com/data/33367 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.