c - 如何从输入文件中读取并保存每行的某些部分并将其输出到命令行?

标签 c string

这是我目前拥有的 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

Phillip Sandoval 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;
}

关于c - 如何从输入文件中读取并保存每行的某些部分并将其输出到命令行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54601592/

相关文章:

c - C 中 printf 的标志和修饰符

c# - 如何将 tabSpace 插入到字符串中?

java - equals() 未按预期工作

c - 如何以编程方式更改选定的文本字体/颜色?

c - fflush在fork操作之前做了什么?

VB.Net-评估字符串中的数学表达式

java - 如何解析具有自定义逻辑条件的字符串?

c# - 给定字符串的特定索引号,如何在 C# 中获取完整的单词?

c - 将不可打印的 ascii 字符输入到 scanf 中

c - 来自变量的 Bash 脚本 printf 颜色?