找回密码
 注册
查看: 912|回复: 10

怎么从文件中依次读取单词

[复制链接]
发表于 2004-10-11 17:11:38 | 显示全部楼层 |阅读模式
唉~~困惑好几天了~郁闷~
 楼主| 发表于 2004-10-11 17:12:08 | 显示全部楼层
strtok()  具体怎么用啊~~
回复

使用道具 举报

发表于 2004-10-11 19:22:47 | 显示全部楼层
man strtok
这个函数我没用过,干什么用的?
回复

使用道具 举报

发表于 2004-10-12 15:05:21 | 显示全部楼层
strtok 是从一个字串读取以指定分割符分割的字段,我是通常这么用的
回复

使用道具 举报

发表于 2004-10-16 16:50:19 | 显示全部楼层
用fscanf或fgets不行吗?
回复

使用道具 举报

发表于 2004-10-18 14:03:55 | 显示全部楼层
做个词法分析器,就行了。
回复

使用道具 举报

发表于 2004-10-18 14:42:53 | 显示全部楼层
下面是我在VC下写的一个简单的测试程序,
你可以自己改成你需要的,
推荐你看一下编译原理,

#include <iostream>
#include <string>
#include <tchar.h>
#include <ctype.h>
#include <fstream>
using namespace std;
#ifndef MAX_WORD_LEN
#define MAX_WORD_LEN 20
#endif

TCHAR ch=' ';

bool not_a_character(TCHAR ch)
{
        bool bRetCode = true;
        if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
                bRetCode = false;
        return bRetCode;
}

typedef enum{ok,fail,end} retType;
retType get_a_word(ifstream& in,TCHAR* result)
{
        retType ret = ok;
        result[0]='\0';
        int index = 0;
       
        while(ch!=EOF&&not_a_character(ch))
        {
                ch = in.get();
                if(ch == EOF)
                {
                        ret = end;
                        break;
                }
        }
        while(ch!=EOF&&index<=MAX_WORD_LEN&&!not_a_character(ch))
        {
                result[index]=ch;
                ch = in.get();
                index++;
        }
        result[index]='\0';
                if(ch == EOF)
                             ret = end;
        return ret;
}

int main(int argc,TCHAR* argv[])
{
        int iRetCode = 0;
//////
       
        TCHAR buffer[MAX_WORD_LEN+1];

//////
        ifstream input;
        input.open(_T("c:\\temp\\test.txt"));
        if(!input.is_open())
        {
                iRetCode = 1;
        }
        else
        {
                while(get_a_word(input,buffer)!=end)
                {
                        cout<<buffer<<endl;
                }
        }
        return iRetCode;
}
回复

使用道具 举报

发表于 2004-10-18 15:42:00 | 显示全部楼层
请问,如何贴代码??
为什么我贴上去的代码都对不齐了?
回复

使用道具 举报

发表于 2004-10-18 22:31:55 | 显示全部楼层
'['code']'
'['/code']'
回复

使用道具 举报

发表于 2004-10-18 22:33:34 | 显示全部楼层
单引号都不要
回复

使用道具 举报

发表于 2004-10-19 08:22:26 | 显示全部楼层
帮你排了一下版
[code:1]
#include <iostream>
#include <string>
#include <tchar.h>
#include <ctype.h>
#include <fstream>
using namespace std;
#ifndef MAX_WORD_LEN
#define MAX_WORD_LEN 20
#endif

TCHAR ch=' ';

bool not_a_character(TCHAR ch)
{
        bool bRetCode = true;
        if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
                bRetCode = false;
        return bRetCode;
}

typedef enum{ok,fail,end} retType;
retType get_a_word(ifstream& in,TCHAR* result)
{
        retType ret = ok;
        result[0]='\0';
        int index = 0;
       
        while(ch!=EOF&&not_a_character(ch))
        {
                ch = in.get();
                if(ch == EOF)
                {
                        ret = end;
                        break;
                }
        }
        while(ch!=EOF&&index<=MAX_WORD_LEN&&!not_a_character(ch))
        {
                result[index]=ch;
                ch = in.get();
                index++;
        }
        result[index]='\0';
       
        return ret;
}

int main(int argc,TCHAR* argv[])
{
        int iRetCode = 0;
        //////
       
        TCHAR buffer[MAX_WORD_LEN+1];
       
        //////
        ifstream input;
        input.open(_T("c:\\temp\\test.txt"));
        if(!input.is_open())
        {
                iRetCode = 1;
        }
        else
        {
                while(get_a_word(input,buffer)!=end)
                {
                        cout<<buffer<<endl;
                }
        }
        return iRetCode;
}
[/code:1]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2025-2-12 04:19 , Processed in 0.023690 second(s), 15 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表