3_Windows下利用批次檔_去除C原始碼中指示行號的前置數字

來源:互聯網
上載者:User

  在園子裡面貼代碼的時候,經常有人貼代碼就像下面這樣前面帶有行號,複製下來後,粘貼到代碼編輯器裡面後,前面很多的數字

編譯前還要全部的刪除這些數字才能編譯,幾十行的代碼刪除也就算了,如果程式碼成千上萬行後就比較麻煩了。

 Exp:帶前置行號的C原始碼

1 #include <stdio.h>
2
3 int main(int argc,char **argv)
4 {
5 printf("hello world!");
6
7 return 0;
8 }

  如果能有一個簡單的方法來去除這些代碼前面的行號,將有利於提高學習效率;既然大家都是懂得編程的人,就會想到用編程的方法來

實現這個功能;比方說利用C語言編寫一個讀寫檔案的小程式來實現這個功能;但是這樣又比較麻煩,那有麼有更簡單的方法呢?

      答案是:有。

  利用命令列提供的功能就能實現。基於上面的需求我用命令列實現的一個簡單的去除C語言代碼中前面的行號批處理。下面給出批處理

檔案的原始碼:

@echo off

title C語言代碼去除前置數字批處理
echo.
echo.
echo ***************************************************************
echo.
echo.
echo 歡迎使用——C語言代碼去除前置數字批處理——工具
echo.
echo.
echo Author: volcanol
echo Version: v0.0
echo QQ: 164498180
echo Email: lilinly225
echo Blog: http://www.cnblogs.com/volcanol/
echo.
echo.
echo ****************************************************************
echo.
echo.
echo 按任意鍵繼續.........
echo.
echo.
pause >nul

::接受使用者輸入檔案名稱
cls
echo.
echo 請輸入完整路徑及檔案名稱(檔案在當前檔案夾可僅輸入檔案名稱):
echo.
set /p file=

::判斷檔案是否存在
if exist %file% goto goon

::不存在要編輯的檔案,提示後退出
echo.
echo.
cls
echo ******************************************************************
echo.
echo.
echo 您要編輯的檔案不存在,請確認路徑及檔案名稱是否正確!
echo.
echo 感謝使用——C語言代碼去除前置數字批處理——工具
echo.
echo 請按任意鍵退出................
echo.
echo.
echo ******************************************************************
pause >nul
exit

::存在檔案就執行下面的語句
:goon
echo. >de_pre_num.c
for /f "tokens=2-30" %%i in ( %file% ) do echo %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v %%w %%x %%y %%z>>de_pre_num.c
cls
echo ******************************************************************
echo.
echo.
echo 已經成功處理檔案,並將處理後的檔案儲存在:de_pre_num.c
echo.
echo 感謝使用——C語言代碼去除前置數字批處理——工具
echo.
echo 請按任意鍵退出.........
echo.
echo.
echo ******************************************************************
pause >nul

Exp:下面是帶前置數字C語言代碼。

 1 /*                 
2 StrToInt: 將字串轉換為整型數
3 */
4 #include <stdio.h>
5 unsigned long int str_to_int(const char *source);
6 int main(int argc char *argv[])
7 {
8 char str[5]="1234";
9 unsigned long int test;
10 test=str_to_int(str);
11 printf("%d\n" test);
12 return 0;
13 }
14 /*
15 函數功能:
16 將一個以0-9字元組成的字串轉換為整型資料
17 函數原型:
18 unsigned long int str_to_int(const char *source)
19 函數參數:
20 char *source: 儲存字串的首地址
21 傳回值:
22 轉換後的Int數值
23 異常:
24 傳遞null 指標或者時函數無法返回正確值
25 */
26 unsigned long int str_to_int(const char *source)
27 {
28 unsigned long int result=0;
29 while('\0'!= *source )
30 {
31 result =(*source + result - '0')*10;
32 ++source;
33 }
34 return result/10;
35 }

Exp:下面是用批處理處理後的C語言代碼:

/*                 
StrToInt: 將字串轉換為整型數
*/
#include <stdio.h>
unsigned long int str_to_int(const char *source);
int main(int argc char *argv[])
{
char str[5]="1234";
unsigned long int test;
test=str_to_int(str);
printf("%d\n" test);
return 0;
}
/*
函數功能:
將一個以0-9字元組成的字串轉換為整型資料
函數原型:
unsigned long int str_to_int(const char *source)
函數參數:
char *source: 儲存字串的首地址
傳回值:
轉換後的Int數值
異常:
傳遞null 指標或者時函數無法返回正確值
*/
unsigned long int str_to_int(const char *source)
{
unsigned long int result=0;
while('\0'!= *source )
{
result =(*source + result - '0')*10;
++source;
}
return result/10;
}

     在學習linux的時候,知道有Regex這麼個玩意,在VB Script或JScript裡面好像也有,都可以用來處理文字檔;以前沒有深入瞭解命令列這個東西,現在

發現Win下的批處理還是不錯的,可以實現很多功能。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.