C Language file operation 03--longest shortest line find and statistics

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.