[英]Reading a multiline text file using sscanf and fgets
我需要读取一个文本文件,然后将结果打印出来。 我使用结构来保存变量,我不需要创建数组。 问题是代码似乎没有读取第二行,或者我打印不正确,结果完全混乱。 这是文本文件:
UTSA01 8 0 150.00 80.00
Armadillos,utsa@xyz.com,(210)555-1111,Jean E Us
COM001 7 1 150.00 75.00
Comm Eagles,maeking@xyz.com,(210)555-2222,Mae King
SOUTH1 5 3 120.00 75.00
Slam Dunk,slamdunk@gmail.com,(210)555-3333,Jerry Tall
ALHGHT 4 4 175.00 100.00
Cake Eaters,sliverspoon@xyz.com,(210)555-6666,E Z Street
UNKN01 1 7 150.00 50.00
Org New Blk,bobwire@xyz.com,(210)555-1234,Bob Wire
NEWB01 0 8 120.00 75.00
River Rats,riverrat@xyz.com,(210)555-4444,Rock D Boat
UNKN02 3 5 150.00 75.00
Hackers,cyber@gmail.com,(210)555-5555,Tom E Gunn
我一直在一个 while 循环中使用 sscanf 和 fgets。 这是我的代码:
char szInputBuffer[100];
/* Create a struct array that will hold the data that is being read in */
Team teams;
/* The following segment contains the header for the file */
printf("%-7s%-13sWins Loss Fee Amt Paid Amt\n", "Id", "Team Name");
printf(" %-21s%-14sEmail\n", "Contact Name", "Phone");
/* The following structure will begin to read file data into the respectful
variables until the end-of-file marker has been reached.
The data will be used later to print out. */
while(!feof(pFileTeam)) {
fgets(szInputBuffer, 100, pFileTeam);
sscanf(szInputBuffer, "%7s %d %d %lf %lf",
teams.szTeamId,
&teams.iWins,
&teams.iLosses,
&teams.dFeeAmount,
&teams.dPaidAmount);
fgets(szInputBuffer, 256, pFileTeam);
sscanf(szInputBuffer, "%[^,]%[^,]%[^,]%[^\n]",
teams.szTeamName,
teams.szEmailAddr,
teams.szPhone,
teams.szContactname);
printf("%-7s%-13s%-5d%-5d%-9.2lf%-8.2lf\n",
teams.szTeamId,
teams.szTeamName,
teams.iWins,
teams.iLosses,
teams.dFeeAmount,
teams.dPaidAmount);
printf(" %-21s%-14s%-5s\n",
teams.szContactname,
teams.szPhone,
teams.szEmailAddr);
}//END while
我遇到的问题是我可以读取和打印文件的第一行,第二行中的第一个字符串很长。 但是,一旦我点击逗号,它似乎就会被绊倒。 我不确定是我在 sscanf 中使用的说明符还是正在打印的说明符。 我真的很困惑我应该做什么。
这是我编译(gcc)并执行时的结果:
Id Team Name Wins Loss Fee Amt Paid Amt
Contact Name Phone Email
UTSA01 Armadillos 8 0 150.00 80.00
COM001 Comm Eagles 7 1 150.00 75.00
SOUTH1 Slam Dunk 5 3 120.00 75.00
ALHGHT Cake Eaters 4 4 175.00 100.00
UNKN01 Org New Blk 1 7 150.00 50.00
NEWB01 River Rats 0 8 120.00 75.00
UNKN02 Hackers 3 5 150.00 75.00
它实际上在我终端的第二行显示了一些奇怪的字符,但它们似乎没有出现在这里。
编辑:我添加了逗号,但输出仍然无效。 只有最后打印的行会显示 szContactname 信息。 所有其他人都将该位置留空,但显示了电话和电子邮件:
Id Team Name Wins Loss Fee Amt Paid Amt
Contact Name Phone Email
UTSA01 Armadillos 8 0 150.00 80.00
(210)555-1111 utsa@xyz.com
COM001 Comm Eagles 7 1 150.00 75.00
(210)555-2222 maeking@xyz.com
SOUTH1 Slam Dunk 5 3 120.00 75.00
(210)555-3333 slamdunk@gmail.com
ALHGHT Cake Eaters 4 4 175.00 100.00
(210)555-6666 sliverspoon@xyz.com
UNKN01 Org New Blk 1 7 150.00 50.00
(210)555-1234 bobwire@xyz.com
NEWB01 River Rats 0 8 120.00 75.00
(210)555-4444 riverrat@xyz.com
UNKN02 Hackers 3 5 150.00 75.00
Tom E Gunn (210)555-5555 cyber@gmail.com
一些问题
使用fgets()
缓冲区而不检查结果。 在尝试使用szInputBuffer
之前检查fgets()
的结果。 不需要进一步的feof()
检查。 @kaylum
// while(!feof(pFileTeam)) { // fgets(szInputBuffer, 100, pFileTeam); // sscanf(szInputBuffer, .... while(fgets(szInputBuffer, sizeof szInputBuffer, pFileTeam) != NULL) { sscanf(szInputBuffer, ....
使用魔术数字。 想想szInputBuffer[]
并没有那么大。
char szInputBuffer[100]; ... // fgets(szInputBuffer, 256, pFileTeam); if (fgets(szInputBuffer, sizeof szInputBuffer, pFileTeam) == NULL) break;
没用的格式。 "%[^,]%[^,]..."
尝试在不读取文件流的情况下读取文本到','
和离开','
的位置。 @用户3121023 。 健壮的代码限制字符串输入并检查sscanf()
结果。
// sscanf(szInputBuffer, "%[^,]%[^,]%[^,]%[^\\n]", teams.szTeamName, ... // Assuming szTeamName[10] if (4 != sscanf(szInputBuffer, "%9[^,],%9[^,],%9[^,],%9[^\\n]", teams.szTeamName, ...) { break; }
也许其他问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.