我用C编写了一个程序,以查找具有最大字符数的行。
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main (int argc, char *argv[])
{
char c; /* used to store the character with getc */
int c_tot = 0, c_rig = 0, c_max = 0; /* counters of characters*/
int r_tot = 0; /* counters of rows */
FILE *fptr;
fptr = fopen(argv[1], "r");
if (fptr == NULL || argc != 2)
{
printf ("Error opening the file %s\n'", argv[1]);
exit(EXIT_FAILURE);
}
while ( (c = getc(fptr)) != EOF)
{
if (c != ' ' && c != '\n')
{
c_tot++;
c_rig++;
}
if (c == '\n')
{
r_tot++;
if (c_rig > c_max)
c_max = c_rig;
c_rig = 0;
}
}
printf ("Total rows: %d\n", r_tot);
printf ("Total characters: %d\n", c_tot);
printf ("Total characters in a row: %d\n", c_max);
printf ("Average number of characters on a row: %d\n", (c_tot/r_tot));
printf ("The row with max characters is: %s\n", ??????)
return 0;
}
我可以很容易地找到字符数最多的行,但是如何打印出来呢?