I need to read a text file, and then print the results out. I am using a struct to hold the variables, and I do not need to create an array. The problem is that the code seems to either not be reading the second line or I'm printing incorrectly, and the results are completely jumbled. Here is the text file:
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
I've been using sscanf and fgets in a while loop. Here is my code:
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
The problem I am having is that I can read and print the first line of the file, long with the first string in the second line. However, it seems to get tripped up once I hit the comma. I'm not sure if it is the specifiers that I am using in the sscanf or if it is printing. I am really confused as to what I should do.
Here are my results when I compile (gcc) and execute:
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
It actually showed some strange characters on the second lines in my terminal, but they don't seem to show up on here.
EDIT: I've added the commas, but the output is still not working. Only the last printed line will show the szContactname information. All of the other ones leave that spot blank, but the phone and email are shown:
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