熟悉多文件操作
//【file_head.h】
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int readData(char filename[]);
void writeData(int n);
//【file_main.h】
#include"file_head.h"
int main(void)
{
char name[40];
int word_num;
puts("Enter the file name:");
gets(name);
word_num = readData(strcat(name , ".txt"));
writeData(word_num);
return 0;
}
//【file_readData】
#include"file_head.h"
int readData(char filename[])
{
FILE* fp;
char ch;
char flag = 0;
int num=0;
if((fp = fopen(filename,"r")) == NULL)
{
printf("%s open failure",filename);
exit(EXIT_FAILURE);
}
printf("%s open sucessfully!~\n",filename);
while(!feof(fp))
{
ch = fgetc(fp);
if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'))
flag = 0;
else if(flag == 0 && (ch != '-' && ch != '/'&& ch != '\''))
{
num++;
flag = 1;
}
}
fclose(fp);
return num;
}
//【file_writeData】
#include"file_head.h"
void writeData(int n)
{
FILE* fp;
if((fp = fopen("D:\\result.txt","a")) == NULL)
{
printf("File open failure!!!");
exit(EXIT_FAILURE);
}
printf("There are %d words.\n",n);
fprintf(fp,"文件中的单词个数为%d\n",n);
printf("结果保存在D:\\result.txt当中\n",n);
fclose(fp);
}
下一步是思考 统计汉字个数 & 做个界面