File
/*
===============================================================
Title: Find the longest and shortest line output from a text file on the screen, and in the statistics file
How many lines are there?
===============================================================
*/
#include <stdio.h>
#include <string.h>
#define N 80
#define HH printf ("===================================================\n");
void Main ()
{
FILE *FP;
Char h[n],zc[n],zd[n],wenjian[20];
int N=0,len,maxno,minno;
printf ("Input file full path: \ n");
HH
scanf ("%s", &wenjian);
Fp=fopen (Wenjian, "R");
if (fp==null)
printf ("File open failed!\n");
Else
{
while (!feof (FP))
{
Fgets (H,N,FP);
if (n==1)
{
Maxno=1;
Minno=1;
strcpy (ZC,H);
strcpy (ZD,H);
}
Else
{
if (strlen (ZC) <=strlen (h))
{
Maxno=n;
strcpy (ZC,H);
}
if (strlen (ZD) >=strlen (h))
{
Minno=n;
strcpy (ZD,H);
}
}
n++;
}
Fclose (FP);
n--;
HH
printf ("Description: File has:%d lines!\n", n); HH
printf ("The longest line of%d characters, the content is: \n%s\n", Maxno,strlen (ZC), ZC); HH
printf ("line%d shortest,%d characters, content: \n%s\n", Minno,strlen (ZD), ZD); HH
}
}
/*
===============================================================
Evaluation:
If you want to output in the behavior unit, define an array representation row, corresponding to the h[n in the program];
n controls the number of characters for a row. strlen () measuring length, strcpy here is actually the assignment function!
The main idea is that the shilling is the longest and shortest of the first row, then compared with the remaining rows
And replace, that is, the longest longer than the longest replacement, the same as the shortest short is replaced by the shortest. Straight
To the end of the file. The reason why n--is because, when the last n+1 found that no new rows, so reduce back
To.
Note: NewLine characters are also one character and will be counted.
===============================================================
*/
Copyright Notice: Bo Main article can be reproduced non-commercial, but please be sure to indicate the source, because the level is limited, inevitably error, in this disclaimer.
C Language file operation 03--longest shortest line find and statistics