Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*count.c
- Author: Sam Dunne
- Description: Counts the number of characters, words and line in a file
- Date: 5/11/2010 */
- /* Edited by Emil 5/11/2010 */
- #include <stdio.h>
- #include <string.h>
- /* These could just as well be declared inside main(), and that is what I would do */
- FILE *fp1;
- char fname[300];
- /* I also turned c into a char instead of an int */
- int lines, space;
- char c;
- int main() /* int main(), main should always return int */
- {
- printf("Enter the name of the file to analysis. \n");
- fgets(fname, sizeof(fname), stdin); /* NEVER use gets() */
- fname[strlen(fname)-1] = '\0'; /* Remove last \n, which fgets() kept */
- fp1 = fopen(fname, "r");
- if ( fp1 == NULL )
- {
- printf("Invalid file name. Please try again. \n");
- }
- else
- {
- printf("File opened successfully. \n");
- c = getc (fp1);
- while ( c != EOF )
- {
- if ( c == '\n')
- {
- c = lines;
- lines++;
- }
- if (c == ' ')
- {
- c = space;
- space++;
- }
- c = getc (fp1); /* get a new char */
- }
- }
- printf("The file contained the following >%d lines \n >%d spaces",lines, space);
- fclose(fp1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement