Advertisement
ASP_Volume2 Debugging and Error Handling #40264

A Simple Count String

Aims to count how many words in the infile

AI

Tóm tắt bởi AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Mã nguồn
original-source
/*****************************************/
/* Program Name : A Simple Count String */
/* Description : Aims to count how many */
/*        words in the infile  */
/* Programmer  : Raymond Li    	 */
/*****************************************/
 
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 256
main(int argc, char *argv[])
{
 char inword[MAX_LENGTH];
 int word_count = 0;
 FILE *infile;
 if (argc < 2)
 {
 printf("Format : wordcnt <filename>\n");
 exit( 1 );
 }
 /* open the infile */
 if ((infile = fopen( argv[1], "rt" )) == NULL)
 {
 printf("Cannot open input file - %s\n", argv[1] );
 exit( 1 );
 }
 /* read the string and count it while not end of file */
 while( !feof(infile) )
 {
 fscanf( infile, "%s", inword );
 word_count++;
 }
 
 /* print the result */
 printf( "There are %d word(s) in the file %s\n", word_count, argv[1] );
 /* close the infile */
 fclose(infile);
 return(0);
}
Upload
Bình luận gốc (3)
Được khôi phục từ Wayback Machine