请写个字符统计函数
#include<stdio.h>#include<string.h>
#include<stdlib.h>
int lines;//文件的长度也是数组的行数
char **array;
char **text_2_array(const char *filename)
{
char *p;
int f_size;
if(filename == NULL) return NULL;
FILE *fp = fopen(filename, "rb");
if(fp == NULL) return NULL;
fseek(fp, 0L, SEEK_END);
f_size = ftell(fp);
//printf("%d\n",f_size);
fseek(fp, 0L, SEEK_SET);
char *buf = (char *) calloc(f_size, sizeof(char));
if(buf == NULL) return NULL;
fread(buf, sizeof(char), f_size, fp);
fclose(fp);
for(p=strchr(buf, '\n'), lines=1; p!=NULL; p=strchr(p, '\n'), lines++)
{
if(*p == '\n') p++;
}
array = (char **) calloc(lines+1, sizeof(char*));
array[0] = buf;
for(p=strchr(buf, '\n'), lines=1; p!=NULL; p=strchr(p, '\n'))
{
if(*p == '\n') *p++ = '\0';
if(p != NULL) array[lines++] = p;
}
array[lines] = NULL;
//printf("%d\n",lines);
return array;
}
这个函数的要求就是对array分别进行每列最后一个字符出现次数的统计。
/******************主函数******************/
int main()
{
FILE *out;
out=fopen("out.txt","w+");
text_2_array("in.txt");
/************** 插函数处 **************/
fclose(out);
return 0;
}
/*******************in.txt**********************/
1235ftgh
3595trlo
1548gjol
2159dser
2548ghyu
2458gtyh
6981wedb
1269fdvt
1489hygg
5896dews
4719hygf
8536fred
5874hygb
9874kiuj
6954gtrf
2147gtff
5841hhyt
erft4589
gtyh5874
gred9872
iolp2536
aswd2154
erfg5843
hyjk1203
xswz1025
5462dwsa
52148693
kjhgy415
4789652k
01235lkh
1458lkiu
2541lyre
9841hgtr
2013hytg
1489hrfd
4715kjun
698kjhyu
1235ftgh
3595trlo
1548gjol
2159dser
2548ghyu
2458gtyh
6981wedb
1269fdvt
1489hygg
5896dews
4719hygf
8536fred
5874hygb
9874kiuj
6954gtrf
2147gtff
5841hhyt
erft4589
gtyh5874
gred9872
iolp2536
aswd2154
erfg5843
hyjk1203
xswz1025
5462dwsa
52148693
kjhgy415
4789652k
01235lkh
1458lkiu
2541lyre
9841hgtr
2013hytg
1489hrfd
4715kjun
698kjhyu
2548ghyu
2458gtyh
6981wedb
1269fdvt
1489hygg
5896dews
4719hygf
8536fred
5874hygb
9874kiuj
6954gtrf
2147gtff
5841hhyt
erft4589
gtyh5874
gred9872
iolp2536
aswd2154
erfg5843
hyjk1203
xswz1025
1269fdvt
1489hygg
5896dews
4719hygf
8536fred
5874hygb
9874kiuj
6954gtrf
2147gtff
5841hhyt
erft4589
gtyh5874
gred9872
iolp2536
aswd2154
erfg5843
hyjk1203
xswz1025
5462dwsa
52148693
kjhgy415
4789652k
01235lkh
1458lkiu
2541lyre
9841hgtr
2013hytg
1489hrfd
4715kjun
698kjhyu
1235ftgh