#include <string.h>
#define MAXSIZE 4000000
struct password {
char psw[12] // 密码名称
int counter // 出现次数计数器
}
int Append(struct password a[], int *n, char psw[]) {
int i
for(i = 0 i < *n ++i) {
if(strcmp(a[i].psw,psw) == 0) {
++a[i].counter
return 2
}
}
if(*n < MAXSIZE) {
strcpy(a[*n].psw,psw)
a[*n].counter = 1
++(*n)
return 1
}
return 0
}
int main() {
struct password a[MAXSIZE]
char psw[12]
int i,n = 0,id
char infilename[] = "indata.txt"
char outfilename[] = "outdata.txt"
FILE *inf,*outf
if((inf = fopen(infilename,"rt")) == NULL) {
printf("不能打开数据文件:%s。\n",infilename)
return 1
}
while(fscanf(inf,"%d %11s",&id,psw) == 2) {
if(Append(a,&n,psw) == 0) break
}
fclose(inf)
if((outf = fopen(outfilename,"wt")) == NULL) {
printf("不能打开数据文件:%s。\n",outfilename)
return 2
}
for(i = 0 i < n ++i)
fprintf(outf,"%s %d\n",a[i].psw,a[i].counter)
fclose(outf)
return 0
}
写文件一般是追加快些如果你要输出例如XML。
还有,每个进程应该有4G,靠 *** 作系统内存管理+页面文件维持、
还有,开个几百M的数组不划算、用malloc+free快些,可以动态的,不让 *** 作系统在加载时就开辟几百M,而是陆续的开——除非你在写汇编程序——汇编向OS申请内存很费劲,要 SYSCALL
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)