一个关于C语言文件读取的问题

来源:百度知道 编辑:UC知道 时间:2024/05/20 21:22:14
我现在想用C语言实现这样一个功能,让程序从本地磁盘读取一个文本文件,分析这篇文章中某个单词出现的数目,并返回这个数目,请问该如何处理

这是我们前一久做的题,跟你的差不多

#include "stdafx.h"
#include <fstream.h>
#include <string>
using namespace std;

long words = 0,charac = 0,parag = 1;

struct node {

struct node* next;
char data[21];
int len;
};
void count(FILE *temp)
{
char q = 1;
char *p = new char[1];
for(long i = 0;*p != 0;i ++)
{
if(!fread(p,1,1,temp))
{
if(q != ' ' && q != '\n')
words ++;
break;
}
if(*p == ' ' && i != 0 && q != ' ' && q != '\n')
{
words ++;
}
else if(*p == '\n')
{
parag ++;
if(i != 0 && q != '\n' && q != ' ')
words ++;
}