[英]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.这是我到目前为止的 C 代码。 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 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 vbjensen 维纳斯詹森 (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.我的问题是,我得到了正确的输出,但是对于 (1) 没有 FRNO 或等效的东西和 (2) 没有 @ 符号的某些行,该行仍然显示。 For example, the lines:
例如,以下几行:
Noe Richard 974927158 nirichar@bvu.edu 079-651-3667 HAVQ诺伊·理查德 974927158 nirichar@bvu.edu 079-651-3667 HAVQ
Phillip Sandoval 836145561 pusandov#luu.edu OXRU 697-728-1807 LHPN GUX菲利普·桑多瓦尔 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.不应打印这些行,因为第一行没有 FRNO 等效项,第二行没有 @ 符号。 Every time I try to add the format operation to match but not save, the program sscanf function starts to mess up.
每次我尝试添加格式化操作来匹配但不保存时,程序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;
}
In the format string "%20s%20s%*[ 0-9]%20[^@]@%*s%20s %3c-%3c-%4c"
在格式字符串
"%20s%20s%*[ 0-9]%20[^@]@%*s%20s %3c-%3c-%4c"
%20s
will scan up to 20 non-whitespace characters. %20s
将扫描最多 20 个非空白字符。 Ignoring leading whitespace and stopping at trailing whitespace.忽略前导空格并在尾随空格处停止。
%*[ 0-9]
will scan spaces and digits. %*[ 0-9]
将扫描空格和数字。 The asterisk, *, tells sscanf to discard the scanned characters.星号 * 告诉 sscanf 丢弃扫描的字符。
%20[^@]@
will scan up to 20 characters or will stop scanning at a @
. %20[^@]@
将最多扫描 20 个字符或将在@
处停止扫描。 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. %*s
将扫描非空白并丢弃字符。
%20s
will scan up to 20 non-whitespace characters. %20s
将扫描最多 20 个非空白字符。
%3c
will ignore any leading whitespace and scan three characters. %3c
将忽略任何前导空格并扫描三个字符。
-%3c
will scan a -
and then three characters. -%3c
将扫描一个-
然后三个字符。 If the -
is missing the scan will terminate early.如果
-
缺失,扫描将提前终止。
-%4c
will scan a -
and then four characters. -%4c
将扫描一个-
然后四个字符。 If the -
is missing the scan will terminate early.如果
-
缺失,扫描将提前终止。
If sscanf
does not scan seven items, nothing will be printed.如果
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.