linux系統編程:read,write與lseek的綜合應用

来源:https://www.cnblogs.com/ghostwu/archive/2018/01/10/8261151.html
-Advertisement-
Play Games

這個實例根據命令行參數進行相應的讀學操作: 用法: file參數:文件名, 如果不存在會自動創建 r<length>: 如r5, r: 讀取操作,表示在當前文件指針後面讀取5個位元組的內容,以文本形式輸出. R<length>:如R5 R:讀取操作,表示在當前文件指針後面讀取5個位元組的內容,以十六進位 ...


這個實例根據命令行參數進行相應的讀學操作:

用法:

usage:./io file {r<length>|R<length>|w<string>|s<offset>}...

file參數:文件名, 如果不存在會自動創建

r<length>:  如r5,   r: 讀取操作,表示在當前文件指針後面讀取5個位元組的內容,以文本形式輸出.

R<length>:如R5   R:讀取操作,表示在當前文件指針後面讀取5個位元組的內容,以十六進位形式輸出.

w<string>: 如wghostwu:  w表示寫入操作,表示在當前文件指針後面寫入5個位元組的內容

s<offset>: 如s1000, 從文件開頭把指針移動1000個位元組

源代碼:

  1 /*================================================================
  2 *   Copyright (C) 2018 . All rights reserved.
  3 *   
  4 *   文件名稱:io.c
  5 *   創 建 者:ghostwu(吳華)
  6 *   創建日期:2018年01月10日
  7 *   描    述:write,open,lseek結合示例
  8 *
  9 ================================================================*/
 10 
 11 #include <stdio.h>
 12 #include <stdlib.h>
 13 #include <string.h>
 14 #include <sys/types.h>
 15 #include <sys/stat.h>
 16 #include <fcntl.h>
 17 #include <limits.h>
 18 #include <sys/types.h>
 19 #include <unistd.h>
 20 
 21 //參數字元串轉整數
 22 int str_to_long( char* str );
 23 
 24 int main(int argc, char *argv[])
 25 {
 26     int i = 2;
 27     int fd = -1;
 28     //保存字元串轉整形的結果
 29     int res;
 30     //寫入的位元組數
 31     ssize_t num;
 32     //動態分配的堆記憶體
 33     char* buf;
 34     //讀取的位元組數
 35     int numread;
 36 
 37     if( argc < 3 || strcmp( argv[1], "--help" ) == 0 )  {
 38         printf( "usage:%s file {r<length>|R<length>|w<string>|s<offset>}...\n", argv[0] );
 39         exit( -1 );
 40     }
 41 
 42     fd = open( argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH );
 43     if( fd < 0 ) {
 44         printf( "文件%s打開或者創建失敗", argv[1] );
 45         exit( -1 );
 46     }
 47     
 48     for( i = 2; i < argc; i++ ){
 49         switch( argv[i][0] ){ 
 50             //移動指針, s後面跟移動的位元組數
 51             case 's':
 52                 res = str_to_long( &argv[i][1] );
 53                 if( -1 == res ) {
 54                     printf( "字元串->整形轉換失敗\n" );
 55                     exit( -1 );
 56                 }
 57                 lseek( fd, res, SEEK_CUR );    
 58                 printf( "%s--->指針移動成功\n", argv[i] );
 59                 break;
 60             //寫入文件, w後面跟寫入的內容
 61             case 'w':
 62                 num = write( fd, &argv[i][1], strlen( &argv[i][1] ) );
 63                 if( num == -1 ) {
 64                     printf( "%s寫入失敗\n", argv[i] );
 65                 }
 66                 printf( "%s成功寫入%ld個位元組\n", argv[i], num );
 67                 break;
 68             case 'r': //字元輸出
 69             case 'R': //十六進位輸出
 70                 res = str_to_long( &argv[i][1] );
 71                 if( -1 == res ) {
 72                     printf( "字元串->整形轉換失敗\n" );
 73                     exit( -1 );
 74                 }
 75                 buf = malloc( res );
 76                 if( buf == NULL ){
 77                     printf( "記憶體分配失敗" );
 78                     exit( -1 );
 79                 }
 80                 numread = read( fd, buf, res );
 81                 if( -1 == numread ) {
 82                     printf( "數據讀取失敗\n" );
 83                     exit( -1 );
 84                 }
 85                 if( 0 == numread ) {
 86                     printf( "已經到達文件尾部" );
 87                 }else {
 88                     printf( "%s: ", argv[i] );
 89                     for ( int j = 0 ; j < numread; j ++ ){
 90                         if( 'r' == argv[i][0] ) {
 91                             printf( "%c", buf[j] );
 92                         }else {
 93                             printf( "%02x ", buf[j] );
 94                         }
 95                     }
 96                 }
 97                 break;
 98             default:
 99                 printf( "參數%s必須以[rRws]中的一個開頭\n", argv[i] );
100         }
101     }
102 
103     return 0;
104 }
105 
106 int str_to_long( char* str ) {
107     char* endstr;
108     int res;
109     res = strtol( str, &endstr, 10 );
110     if( (res == LONG_MIN) || (res == LONG_MAX) ) {
111         return -1;
112     }
113     return res;
114 }
View Code

完整的演示效果:

 1 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls
 2 cp  cp.c  cp.c.copy  io  io.c  strtol  strtol.c
 3 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ !g
 4 gcc io.c -o io
 5 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls -l
 6 total 56
 7 -rwxrwxr-x 1 ghostwu ghostwu  9016 1月  10 17:16 cp
 8 -rw-rw-r-- 1 ghostwu ghostwu  1752 1月  10 17:15 cp.c
 9 -rw-rw-r-- 1 ghostwu ghostwu  1752 1月  10 17:16 cp.c.copy
10 -rwxrwxr-x 1 ghostwu ghostwu 13360 1月  10 22:19 io
11 -rw-rw-r-- 1 ghostwu ghostwu  2743 1月  10 22:19 io.c
12 -rwxrwxr-x 1 ghostwu ghostwu  8824 1月  10 20:47 strtol
13 -rw-rw-r-- 1 ghostwu ghostwu   616 1月  10 20:47 strtol.c
14 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt
15 usage:./io file {r<length>|R<length>|w<string>|s<offset>}...
16 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls -l
17 total 56
18 -rwxrwxr-x 1 ghostwu ghostwu  9016 1月  10 17:16 cp
19 -rw-rw-r-- 1 ghostwu ghostwu  1752 1月  10 17:15 cp.c
20 -rw-rw-r-- 1 ghostwu ghostwu  1752 1月  10 17:16 cp.c.copy
21 -rwxrwxr-x 1 ghostwu ghostwu 13360 1月  10 22:19 io
22 -rw-rw-r-- 1 ghostwu ghostwu  2743 1月  10 22:19 io.c
23 -rwxrwxr-x 1 ghostwu ghostwu  8824 1月  10 20:47 strtol
24 -rw-rw-r-- 1 ghostwu ghostwu   616 1月  10 20:47 strtol.c
25 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt s1000 wghostwu
26 s1000--->指針移動成功
27 wghostwu成功寫入7個位元組
28 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ls -l
29 total 60
30 -rwxrwxr-x 1 ghostwu ghostwu  9016 1月  10 17:16 cp
31 -rw-rw-r-- 1 ghostwu ghostwu  1752 1月  10 17:15 cp.c
32 -rw-rw-r-- 1 ghostwu ghostwu  1752 1月  10 17:16 cp.c.copy
33 -rwxrwxr-x 1 ghostwu ghostwu 13360 1月  10 22:19 io
34 -rw-rw-r-- 1 ghostwu ghostwu  2743 1月  10 22:19 io.c
35 -rwxrwxr-x 1 ghostwu ghostwu  8824 1月  10 20:47 strtol
36 -rw-rw-r-- 1 ghostwu ghostwu   616 1月  10 20:47 strtol.c
37 -rw-rw-r-- 1 ghostwu ghostwu  1007 1月  10 22:20 test.txt
38 ghostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt r1007
39 r1007: ghostwughostwu@ubuntu:~/c_program/tlpi/chapter4$ ./io test.txt R1007
40 R1007: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 1 private string GetAccountNo() 2 { 3 try 4 { 5 string shortName="B"; 6 string latestAccountNO = shortName + "000001"; ... ...
  • 1、引用NuGet: Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerGen 或 2、Startup文件引用Swagger 3、對需要進行授權登錄的介面生成對應的文檔輸入框,如Bearer Token 引用該類: 4、點擊項目 ...
  • int sNum = 0; string s = "100"; int Result = 0; if(int.TryParse(s, out Result)==1)//1轉換成功 0失敗 { sNum=int.TryParse(s); } ...
  • 本文章為 Dictionary<K,V>雙列集合開發項目,如需要List<T>單列集合開發的此項目,請到樓主博客園尋找 博客網址:http://www.cnblogs.com/lsy131479/ 窗體 一.首先創建項目類 二.創建套餐類 三.主窗體代碼 ...
  • 本文章為List<T>單列集合開發項目,如需要 Dictionary<K,V>雙列集合開發的此項目,請到樓主博客園尋找 博客網址:http://www.cnblogs.com/lsy131479/ 窗體 一.首先定義項目類 二.定義套餐類 三.主窗體代碼 ...
  • 1、進入微信公眾號後臺設置微信伺服器配置參數(註意:Token和EncodingAESKey必須和微信伺服器驗證參數保持一致,不然驗證不會通過)。 2、設置為安全模式 3、代碼實現(主要分為驗證介面和消息處理介面): 加解密實現(微信公眾號官網有源碼) ...
  • 奇進偶不進解釋 代碼 1 public class IsopsephyNumber 2 { 3 private double value = 0.0; 4 private bool isFlag = false; 5 /// <summary> 6 /// 奇進偶不進 7 /// </summary ...
  • windows 10秋季創意者更新版1709發佈已經有段時間了,也有很多用戶選擇升級這次更新的系統。那麼,這次Win 10 更新版1709有哪些新功能值得關註呢?下麵,一起隨主機吧來看一看吧! 1、 NEON改名了 有關下一代Win10將全面加入“毛玻璃”的消息,早已在業界盛傳。而它的正式名稱“Fl ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...