Linux下查看txt文檔,Linux查看txt文檔

來源:互聯網
上載者:User

Linux下查看txt文檔,Linux查看txt文檔
        當我們在使用Window作業系統的時候,可能使用最多的文本格式就是txt了,但是當我們將Window平台下的txt文字文件拷貝到Linux平台下查看時,發現原來的中文全部變成了亂碼。沒錯, 引起這個結果的原因就是兩個平台下,編輯器對預設的編碼格式是不一樣的:

在Window平台下,Notepad的預設編碼是ASCII碼或者GBK,而在Linux平台下預設的是UTF-8(中文環境的情況),編碼的不同導致了原來文檔中的中文變成了亂碼。

解決辦法:
使用iconv命令將文檔的編碼進行轉換即可。

iconv預設情況下,是沒有被安裝的,下面簡單介紹下iconv的安裝過程:
1. 下載:
http://www.gnu.org/software/libiconv/#TOCdownloading

2. 安裝:
下載完成後,切換到下載目錄先進行解壓:

$tar -xzvf libiconv-1.14.tar.gz

然後進入解壓後的檔案中

$cd libiconv-1.14_2
查看其中的README檔案,我們可以看到安裝步驟:(當然,如果您熟悉源碼的安裝,這步完全可以省略^-^)
$ ./configure --prefix=/usr/local$ make$ make install

3. 命令學習
該工具安裝完成後,肯定要先瞭解下這個命令的用法吧,這個沒什麼可說的:
$iconv --help
我們會看到下面的內容:
Usage: iconv [OPTION...] [FILE...]Convert encoding of given files from one encoding to another. Input/Output format specification:  -f, --from-code=NAME       encoding of original text  -t, --to-code=NAME         encoding for output Information:  -l, --list                 list all known coded character sets Output control:  -c                         omit invalid characters from output  -o, --output=FILE          output file  -s, --silent               suppress warnings      --verbose              print progress information  -?, --help                 Give this help list      --usage                Give a short usage message  -V, --version              Print program versionMandatory or optional arguments to long options are also mandatory or optionalfor any corresponding short options.
說的很明白,就是按照下面的格式進行轉換:
iconv -f 原編碼 -t 目標編碼 要轉換的檔案

4. 編碼轉換:
學會了編碼的轉化,我們就舉了例子示範一下:
$iconv -f gbk -t utf8 test.txt
命令執行完成後,你發現原來test.txt中的中文正常顯示了,但是開啟原來的檔案,卻發現還是亂碼,這個Easy,我們將輸出的內容輸入到檔案中即可。
$iconv -f gbk -t utf8 test.txt -o test
或者執行下面的命令:
$iconv -f gbk -t utf8 test.txt < test
此時我們開啟這個test檔案就會發現,原來的中文顯示正常了^-^

注意:
如果不出意外的話,上面的安裝步驟可沒有那麼順利,在make的時候,會提示下面的錯誤:
n file included from progname.c:26:0:./stdio.h:1010:1: error: ‘gets’ undeclared here (not in a function) _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead"); ^make[2]: *** [progname.o] Error 1make[2]: Leaving directory `/home/freeman/Downloads/libiconv-1.14_2/srclib'make[1]: *** [all] Error 2make[1]: Leaving directory `/home/freeman/Downloads/libiconv-1.14_2/srclib'make: *** [all] Error 2
這個這個軟體本身存在的一個Bug,通過Google,發現一個解決該問題的補丁,內容如下:
--- srclib/stdio.in.h.orig      2011-08-07 16:42:06.000000000 +0300+++ srclib/stdio.in.h   2013-01-10 15:53:03.000000000 +0200@@ -695,7 +695,9 @@ /* It is very rare that the developer ever has full control of stdin,    so any use of gets warrants an unconditional warning.  Assume it is    always declared, since it is required by C89.  */-_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");+#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)+ _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");+#endif #endif
PS:內容中的"+"表示新增的內容,"-"表示刪除的內容!

那我們只要進行如下操作即可解決這個問題:
1. 切換到srclib目錄下:
$cd srclib

2. 修改stdio.in.h檔案:

$gedit stdio.in.h
通過搜尋,定位到_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");這一行,然後在這一行的前後加上條件編譯即可,修改後的內容如下:

#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)        _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");#endif

3. 儲存退出,然後再進行make, make install便可順利安裝^-^

參考資料:http://forum.z27315.com/topic/15662-%E8%A7%A3%E5%86%B3%E7%BC%96%E8%AF%91libiconv%E6%97%B6%E7%9A%84gets-undeclared-here%E9%94%99%E8%AF%AF/

Linux系統下怎開啟TXT檔案?

nano <檔案名稱> 或者
vi <檔案名稱>
主要大小寫敏感,推薦使用前者,介面比較友好。
 
linux下的純文字文檔到windows下怎看?

你用wordpad開啟能解決換行問題,但是編碼問題就難說了,linux多半是utf8,大概到了vista就能顯示了吧,vista編碼是utf8,以前的windows都是gb2312,gb13080
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.