简体   繁体   中英

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

This is the C code I have so far. I am reading the first name and last name from the input file but the thing that is giving me trouble is to print out the other stuff.

I have to take a line like this:

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

and remove the extra stuff to make it like this:

vbjensen Venus Jensen (624)771-4676

My problem is that I am getting the right output but for some of the lines that(1) don't have the FRNO or something equivalent and (2) not having the @ symbol, the line still shows up. For example, the lines:

Noe Richard 974927158 nirichar@bvu.edu 079-651-3667 HAVQ

Phillip Sandoval 836145561 pusandov#luu.edu OXRU 697-728-1807 LHPN GUX

These lines should not be printed since the first one does not have the FRNO equivalent and the second one does not have the @ symbol. Every time I try to add the format operation to match but not save, the program sscanf function starts to mess up.

  #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;
 }

In the format string "%20s%20s%*[ 0-9]%20[^@]@%*s%20s %3c-%3c-%4c"
%20s will scan up to 20 non-whitespace characters. Ignoring leading whitespace and stopping at trailing whitespace.
%*[ 0-9] will scan spaces and digits. The asterisk, *, tells sscanf to discard the scanned characters.
%20[^@]@ will scan up to 20 characters or will stop scanning at a @ . Then it will try to scan a @ . If the @ is missing the scan will terminate early.
%*s will scan non-whitespace and discard the characters.
%20s will scan up to 20 non-whitespace characters.
%3c will ignore any leading whitespace and scan three characters.
-%3c will scan a - and then three characters. If the - is missing the scan will terminate early.
-%4c will scan a - and then four characters. If the - is missing the scan will terminate early.
If sscanf does not scan seven items, nothing will be printed.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM