簡體   English   中英

如何從輸入文件中讀取並保存每行的某些部分並將其輸出到命令行?

[英]How to read from an input file and save certain parts of each line and output it to the command line?

這是我到目前為止的 C 代碼。 我正在從輸入文件中讀取名字和姓氏,但給我帶來麻煩的是打印出其他內容。

我必須采取這樣的路線:

Venus Jensen 33770530841 vbjensen@oqtu.edu FRNO 624-771-4676 SIJ SBE WHV TVW

並刪除多余的東西,使其像這樣:

vbjensen 維納斯詹森 (624)771-4676

我的問題是,我得到了正確的輸出,但是對於 (1) 沒有 FRNO 或等效的東西和 (2) 沒有 @ 符號的某些行,該行仍然顯示。 例如,以下幾行:

諾伊·理查德 974927158 nirichar@bvu.edu 079-651-3667 HAVQ

菲利普·桑多瓦爾 836145561 pusandov#luu.edu OXRU 697-728-1807 LHPN GUX

不應打印這些行,因為第一行沒有 FRNO 等效項,第二行沒有 @ 符號。 每次我嘗試添加格式化操作來匹配但不保存時,程序sscanf函數開始混亂。

  #include <stdio.h>
  #include <stdbool.h>
  #include <stdlib.h>

   int main()
   {

    // Open the input file and print an error message if we're unsuccessful.
   // (the error message is mostly to help you with debugging.  We won't test
     // this behavior).
     FILE *fp = fopen("input.txt", "r");
      char line[500];
      if(!fp) {

       printf("Can't open input file\n");

      exit(1);
      }

      // Counting input lines, so we can report errors.


     // Keep reading input lines until we reach the end-of-file.
    // Write an output line or an error message for each one.
    do {
      int lineCount = 1;

              char fName[12];
              char lName[12];
             //char skipNum[12];
             char email[9];
            //char firstNum[4];
           //char secondNum[4];
          //char thirdNum[5];
         //printf("%c", ch);

         char phone[] = "(123)123-1234";

        //fscanf(fp, "%s", fName);

       //fscanf(fp, "%s", lName);

      //fscanf(fp, "%[1-9]", skipNum);

      //fscanf(fp, "%[a-z]", email);      



      sscanf (line, "%11s%11s%*[ 0-9]%9[^@]%*[^0-9]%3c-%3c-%4c", lName,   fName, email, &phone[1], &phone[5], &phone[9]);

         //printf("Invalid line");
        //printf("\n");

       // exit(1);

       printf("%s", line);

       printf("\n");

       printf("%s", email);
       printf("%s", fName);
       printf("%s", lName);
      //printf("%s", skipNum);
     //printf("%s", firstNum);

     printf("%s", phone);



    printf("\n");

  lineCount++;
} 
 while (fgets(line, sizeof line, fp));

     return EXIT_SUCCESS;
 }

在格式字符串"%20s%20s%*[ 0-9]%20[^@]@%*s%20s %3c-%3c-%4c"
%20s將掃描最多 20 個非空白字符。 忽略前導空格並在尾隨空格處停止。
%*[ 0-9]將掃描空格和數字。 星號 * 告訴 sscanf 丟棄掃描的字符。
%20[^@]@將最多掃描 20 個字符或將在@處停止掃描。 然后它會嘗試掃描@ 如果缺少@ ,掃描將提前終止。
%*s將掃描非空白並丟棄字符。
%20s將掃描最多 20 個非空白字符。
%3c將忽略任何前導空格並掃描三個字符。
-%3c將掃描一個-然后三個字符。 如果-缺失,掃描將提前終止。
-%4c將掃描一個-然后四個字符。 如果-缺失,掃描將提前終止。
如果sscanf不掃描七個項目,則不會打印任何內容。

#include <stdio.h>
#include <stdlib.h>
int main ( void) {
    char line[500] = "";
    int lineCount = 0;
    FILE *fp = NULL;

    if ( NULL == ( fp = fopen("input.txt", "r"))) {
        fprintf( stderr, "Can't open input file\n");
        exit(1);
    }

    while ( fgets ( line, sizeof line, fp)) {//read each line from the file
        char fName[21];
        char lName[21];
        char match[21];
        char email[21];
        char phone[] = "(123)567-9012";

        lineCount++;
        if ( 7 == sscanf ( line, "%20s%20s%*[ 0-9]%20[^@]@%*s%20s %3c-%3c-%4c"
        , lName, fName, email, match, &phone[1], &phone[5], &phone[9])) {
            printf ( "line [%d] %s %s %s %s\n", lineCount, email, fName, lName, phone);
        }
    }
    fclose ( fp);
    return 0;
}

暫無
暫無

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

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