簡體   English   中英

從不同行大小的輸入文件中讀取行

[英]Read lines from an input file of varying line sizes

目前,我正在使用 getline 從文件中讀取行,我可以通過以下方式從標准輸入訪問單個字符:

char buffer[1024];
while((lineSize = getline(&line, &len, stdin)) != -1) {
            if (line[0] != 84) {
                // ...
                continue; // continue to next line in file
            }
            if (line[0] == 84){ // (the 'T' character)
                printf("TEST: Line Value: %s\n", line);
                buffer[0] = line[1]; // this is a single digit number in char form
                buffer[1] = '\0';
                // send buffer somewhere
                send(clientSocket, buffer, strlen(buffer), 0);
                // ...
}

示例文件如下:

T3
T9
S0
S4
T55
T6

但是,如您所見,當給出大於 9 的數字(例如此處的 T55 行)時,我遇到了問題。 我只能用這種方法獲取第一個數字。 因此,我可能不得不完全重做我讀取文件的方式。 有沒有更好更簡單的方法可以讀取輸入文件並檢查第一個字符並將剩余字符轉換為 int 直到行尾? (最大 integer 可以是 100 btw)

繼續我的評論,您可以使用fgets()讀取line ,然后使用格式字符串為" %c%d%n"sscanf()提取第一個字符並將下一組數字轉換為int ,最后使用"%n"說明符獲取sscanf()在該轉換中消耗的字符總數。 您驗證字符和 integer 都發生了轉換,並且讀取的第一個非空白字符是'T' 然后,您可以根據需要使用mycharmyint ,並使用mylen作為與send一起使用的長度。

注意:您可以line向前掃描以確定開頭是否包含任何空格,並忽略您對send()的調用 - 這是留給您的)

總而言之,您可以這樣:

  char line[1024],
       mychar = 0;
  int myint = 0,
      mylen;
  
  /* read using fgets */
  while (fgets (line, sizeof line, stdin)) {
    /* parse with sscanf, validate both conversions and 'T' as 1st char,
     * use "%n" to get number of chars through int conversion
     */
    if (sscanf (line, " %c%d%n", &mychar, &myint, &mylen) != 2 || 
                mychar != 'T') {
      fputs ("error: invalid format.\n", stderr);
      continue;
    }
    
    send (clientSocket, line, mylen, 0);    /* send mylen chars */
  }

更具體地說,我需要查看您的最小完整可重現示例,以確保您發布的內容之外沒有任何內容會影響上面的代碼。


添加示例

添加一個簡短示例以顯示在預期和意外輸入下使用上述內容進行解析的結果,並添加向前掃描以刪除行中的前導空格,這是一個從stdin讀取輸入並寫入stdout的小程序,輸出匹配的行'T'(int) ,你可以這樣做:

#include <stdio.h>
#include <unistd.h>
#include <ctype.h>

int main (void) {

  char line[1024],
       mychar = 0,
       nl = '\n';
  int myint = 0,
      mylen;
  
  /* read using fgets */
  while (fgets (line, sizeof line, stdin)) {
    char *p = line;
    /* parse with sscanf, validate both conversions and 'T' as 1st char,
     * use "%n" to get number of chars through int conversion
     */
    if (sscanf (line, " %c%d%n", &mychar, &myint, &mylen) != 2 || 
        mychar != 'T') {
      fprintf (stderr, "error: invalid format: %s", line);
      continue;
    }
    
    while (isspace (*p)) {    /* reamove leading whitespace */
      p += 1;
      mylen -= 1;
    }
    
    // send (clientSocket, p, mylen, 0);    /* send mylen chars */
    write (STDOUT_FILENO, p, mylen);
    write (STDOUT_FILENO, &nl, 1);
  }
}

注意: write (STDOUT_FILENO, &nl, 1);只是包含在上面的 output 中,每個 output 之后都有一個換行符——它不會成為你通過套接字send()的內容的一部分——除非接收程序正在使用'\n'作為行終止符)

示例輸入文件:

$ cat dat/charint.txt
T4
T44
T444
 TT
 T3 and more gibberish
P55

注意:'T'開頭的最后兩行中包含前導空格和尾隨字符,包括無效的行格式" TT"

示例使用/輸出

$ ./bin/charandintsend < dat/charint.txt
T4
T44
T444
error: invalid format:  TT
T3
error: invalid format: P55

如果您有任何問題,或者我誤解了您問題的某些方面,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM