C语言编程!帮帮忙!读写文件tof函数c语言,教科书上有 。都写出来是你做作业还是我做作业?
还有别乱用叹号,这不是问人tof函数c语言的态度
只给出关键代码 。请自行阅读并组合 。
0.读入文件
1.分割文件成为token(关键点)
2.统计token数目
3.输出统计信息
1.分割token用C标准库中的strtok()函数完成 。
strtok()的原型为tof函数c语言:
char *strtok( char *strToken, const char *strDelimit );
strToken为目标字符串,strDelimit为分割符集合 。返回值是一个分割好的Token 。
例子程序:
#include string.h
#include stdio.h
char string[] = "A string\tof ,,tokens\nand some more tokens"; // 目标字符串
char seps[] = " ,\t\n";// 分割符集
char *token;
void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* 将目标字符串和分割符集给入strtok,返回第一个token */
token = strtok( string, seps );
while( token != NULL )
{
/* 打印一个token */
printf( " %s\n", token );
/* 获取下一个token */
token = strtok( NULL, seps );
}
}
输出结果如下:
moose@iBookG4:~/Projects/svn_repo/trunk/data_struct/glibc$ ./a.out
A string of ,,tokens
and some more tokens
Tokens:
A
string
of
tokens
and
some
more
tokens
可见所有单词都被分割出来了 。
另外,ANSI还可以用wcstok()对汉字(宽字符集)进行分割 。
再另,如果你的作业考查点在实现一个自己的strtok(),请自行分析之 。
其他几个功能都是比较基本的功能 。不再赘述 。请自行思考 。
给出一个我做的样本,很多情况没有考虑(不用文件输入/输出,不考虑内存消耗,不考虑token数量等等),仅作参考:
#include string.h
#include stdio.h
char string[] = "DESCRIPTION\nFor strtok(): [CX] [Option Start] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard. [Option End]\nA sequence of calls to strtok() breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a byte from the string pointed to by s2. The first call in the sequence has s1 as its first argument, and is followed by calls with a null pointer as their first argument. The separator string pointed to by s2 may be different from call to call.\nThe first call in the sequence searches the string pointed to by s1 for the first byte that is not contained in the current separator string pointed to by s2. If no such byte is found, then there are no tokens in the string pointed to by s1 and strtok() shall return a null pointer. If such a byte is found, it is the start of the first token.\nThe strtok() function then searches from there for a byte that is contained in the current separator string. If no such byte is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token shall return a null pointer. If such a byte is found, it is overwritten by a null byte, which terminates the current token. The strtok() function saves a pointer to the following byte, from which the next search for a token shall start.\nEach subsequent call, with a null pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above.";
char seps[] = " ,.[]:\t\n";
char *token;
struct _token_stat{
char token[64];
int stat;
};
// 至多允许出现1024个单词
struct _token_stat gTokens[1024];
void main( void )
{
int i;
for (i = 0; i1024; i++)
{
gTokens[i].token[0]='\0';
推荐阅读
- excel如何打表签贴纸,表格制作标签贴纸
- 如何利用好新媒体宣传,如何利用新媒体宣传山西本土文化
- erp系统对企业发展的意义,谈谈erp给企业带来的效益
- 红蜂ios固件如何刷新,黄蜂2固件升级失败
- 关于python是函数式语言的信息
- 移动硬盘怎么拷文件,移动硬盘怎么复制到移动硬盘
- 布衣直播软件是什么意思,布衣最新版
- 华为x30i怎么升级鸿蒙系统,x30pro升级安卓10
- linux删除程序命令 linux删除软件命令