📄 finfo.c
字号:
/************************************************************** *
* Using the knowledge of strings, need to run given code through *
* with error checking and counting select characters. * *
*****************************************************************/
#include <stdio.h> //This is all the code given to not be edited
#include <string.h>
#include <ctype.h>
#define MAX_SIZE_FN 50
#define MAX_SIZE_LINE 80
void get_filename(char fn[]);
void print_menu();
int get_info(char fn[]);
int view_file(char fn[]);
int main() {
char line[MAX_SIZE_LINE]; // need to store null char at end
char filename[MAX_SIZE_FN];
char op;
print_menu(); // Print the menu before anything else
do {
printf("Operation: "); // Prompt
fgets(line, sizeof(line), stdin); // Read a line
op = line[0]; // Use only 1st char
switch (op) {
case 'h':
print_menu();
break;
case 'i':
get_filename(filename);
get_info(filename);
break;
case 'v':
get_filename(filename);
view_file(filename);
break;
}
} while (op != 'q');
printf("That's all, folks!\n");
return 0;
}
void print_menu() //this function basically prints off the following menu options followed by the prmompt to press q of h
{
printf(" h: print this menu\n");
printf(" q: quit\n");
printf(" i: get information about a text file\n");
printf(" v: view a text file\n");
}
void get_filename(char fn[]) //this function is for typing in the filename as a string and in this function it takes it in as a string.
{
printf("Filename: ");
fgets(fn,MAX_SIZE_FN,stdin); //make sure you get all of the name of the filename before it can open it.
fn[strlen(fn)-1 ]='\0';
}
int view_file(char fn[]) //for this function, it is going to take the string and run it through error proofing code and print off the contents of it.
{
char line[MAX_SIZE_LINE];
FILE *input_file;
char b;
char c;
char d;
b=fn[strlen(fn)-3]; //this is taking the string in and counting the length of the last 3
c=fn[strlen(fn)-2]; // it then stores those into chars for comparison.
d=fn[strlen(fn)-1];
if (d != 't' && c != 'x' && b != 't' ) //test for the final characters to be .txt
{
printf("%s doesn't end with .txt\n",fn);
}
else
{
input_file = fopen(fn,"r");
if (input_file == NULL) //at this point, see if it can be read by testing for the NULL
{
printf("Can't open %s\n",fn); //If indeed NULL then it can not open
}
else
{
while(fgets(line,sizeof(line),input_file) !=NULL) //continue to print off the lines until there arent any
{
printf("%s",line);
}
}
}
}
int get_info(char fn[])
{
char line[MAX_SIZE_LINE];
FILE *input_file;
char b; //this is the same error proofing as before only with a different code whn all error checking is complete
char c;
char d;
int i=0;
int count=0;
int countchar=0;
int linecount=0;
b=fn[strlen(fn)-3];
c=fn[strlen(fn)-2];
d=fn[strlen(fn)-1];
// set an array with all of the constants that can be checked over as characters.
char cons[20] = {'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z'};
//set and array with all the vowel possibilities that are in the text
char vowl[6] = {'a','e','i','o','u','y'};
int countconst = 0;
if (d != 't' && c != 'x' && b != 't' ) //test for the final characters to be .txt
{
printf("%s doesn't end with .txt\n",fn);
}
else
{
input_file = fopen(fn,"r"); //at this point, see if it can be read by testing for the NULL
if (input_file == NULL)
{
printf("Can't open %s\n",fn);
}
else
{
while(fgets(line,sizeof(line),input_file) !=NULL)
{
int k=0;
int x=0;
linecount++; //its already runninig through line by line just keep a counter
for(x=0; x<strlen(line); x++ )
{
for (k=0; k < 6; k++) //for vowels we want to check for up to 6 values for all the possibilites
{
if(tolower(line[x]) == vowl[k]) //when it finds them, increment the counter
{
count++;
break;
}
}
int j=0;
for(j=0; j < 20; j++) //for consenonants we want to check for up to 20 values for all the possibilites
{
if(tolower(line[x]) == cons[j]) //when it finds them, increment the conscounter
{
countconst++;
break;
}
}
}
}
}
if( linecount == 0) //here check for anything in the file
{
printf("That file is empty.\n"); //if not its empty
}
else //otherwise print off all the counters
{
printf("Lines: %d\n",linecount);
printf("Vowels: %d\n",count);
printf("Consonants: %d\n",countconst);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -