复习了C语言,看了《征服C指针》编程上一个例子word_count 试了下,但稍微有点小问题
main.c 主程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "get_word.h"
#include "word_initialize.c"
#include "add_word.c"
#include "dump_word.c"
#include "word_finalize.c"
#define BUF_SIZE 1024
/*这个程序将输出文件的每个单词的频率,该文件名将从用户输入读取
*/
int main()
{
char file_input[BUF_SIZE],file_output[BUF_SIZE],buf[BUF_SIZE];
int word_len;
FILE *fp_in,*fp_out;
printf("请输入一个被统计的文件的文件名(包括文件后缀),stdin表示从命令行读入:");
scanf("%s",file_input);
printf("请输入统计数据的输出文件的文件名(包括文件后缀),stdout表示从命令行输出:");
scanf("%s",file_output);
/*处理输入的文件名*/
if(!strcmp(file_input,"stdin")) //strcmp函数在两个字符串相同时返回0
{
fp_in = stdin;
}
else
{
if((fp_in=fopen(file_input,"r"))==NULL)
{
printf("open input file error!\n");
exit(1);
}
}
/*处理输出文件名*/
if(!strcmp(file_output,"stdout"))
{
fp_out = stdout;
}
else
{
fp_out = fopen(file_output,"w");
}
/*初始化单词管理数据库*/
word_initialize();
/*读取单词并加入单词管理数据库*/
while((word_len=get_word(buf,BUF_SIZE,fp_in)) != EOF)
{
if(word_len!=0)
{
add_word(buf);
}
}
/*输出单词出现的频率*/
dump_word(fp_out);
word_finalize();
}
word_manage.h
#ifndef WORD_MANAGE_H
#define WORD_MANAGE_H
#include <stdio.h>
typedef struct word_tag
{
char *word;
int count; //单词数目
struct word_tag *next_word;
}WORD;
extern WORD *word_head;
void word_initialize(void);
void add_word(char *word);
void dump_word(FILE *fp);
void word_finalize(void);
#endif
word_initialize.c
#include "word_manage.h"
WORD *word_head = NULL; //这个变量将在word_manage.h中被引用
void word_initialize(void)
{
word_head=NULL;
}
add_word.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "word_manage.h"
/*创建一个新的结点*/
static WORD *create_word(char *word) {
char *temp;
WORD *new_word;
new_word=malloc(sizeof(WORD));
temp=strdup(word); //char *strdup(const char *str)用于返回str字符串的副本,这个副本和str具有不同的内存地址空间
new_word->word=temp; //此处不可以直接将word的指针地址赋给新建的单词
new_word->count=1;
new_word->next_word=NULL;
return new_word;
}
void add_word(char *word)
{
WORD *pos,*pre,*new_word; //pos指向当前结构体,pre指向当前结构体的上一个结构体
pos=word_head;
pre=NULL;
int result;
/*如果不是第一个,则循环直到找到插入点或同样的单词*/
while(pos!=NULL)
{
if((result=strcmp(pos->word,word))<0)
{
pre=pos;
pos=pos->next_word;
}
else break;
}
/*找到同样的单词*/
if(word_head != NULL && result ==0 )
{
pos->count++;
}
else
{
/*插入到第一个单词结点*/
if(pre==NULL)
{
new_word = create_word(word);
new_word->next_word=word_head;
word_head=new_word;
}
/*插入到其他点*/
else
{
new_word=create_word(word);
new_word->next_word=pos;
pre->next_word=new_word;
}
}
}
dump_word.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "word_manage.h"
void dump_word(FILE *fp)
{
WORD *pos;
for(pos = word_head;pos;pos = pos->next_word) {
fprintf(fp,"%-20s%5d\n",pos->word,pos->count);
}
}
word_finalize.c
#include <stdlib.h>
#include "word_manage.h"
void word_finalize(void)
{
WORD *temp;
temp=word_head;
while(temp!=NULL)
{
word_head=word_head->next_word;
free(temp->word);
free(temp);
temp=word_head;
}
}
get_word.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/*
这个函数从输入文件fp获取单词,参数buf指向获取的单词,返回值返回单词长度或是EOF提示
*/
int get_word(char *buf,int buf_size,FILE *fp)
{
int ch,len;
len=0;
while((ch=getc(fp))!=EOF&&isalnum(ch)) //isalnum(int c)函数在c是数字或字母时返回ture
{
buf[len]=ch; //buf[len] is a syntax sugar of *(buf+len)
if(len>=buf_size)
{
printf("Error:word too long!"); //单词长度大于buf_size时发出警报并退出程序
exit(1);
}
len++;
}
buf[len]='\0'; //给字符串添加结束标志
if(ch==EOF)
{
return EOF;
}
else
{
return len;
}
}
/*get_word.h*/
#ifndef GET_WORD_H
#define GET_WORD_H
/*这个函数用于读取fp所指向文件的单词,读取成功则返回单词长度,到达文件尾部返回EOF*/
int get_word(char *buf,int buf_size,FILE *fp);
#endif
问题: 1、在main.c 文件中,看例子只 include get_word.h 和 word_manage.h 两个文件,而我这必须要把所有的 文件要include 进来才能执行
2、读取文件时并不在第一行读取,而是从第二行读取了。




