#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 = 0i <*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 = 0i <n++i)
fprintf(outf,"%s %d\n",a[i].psw,a[i].counter)
fclose(outf)
return 0
}
估计可能是数组越界,修改如下:
int Append(struct password a[], int *n, char psw[]) {
int i
for(i = 0i <*n &&i <MAXSIZE++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 CountLines(char *filename) { ifstream ReadFileint n=0char line[512]ReadFile.open(filename,ios::in)//ios::in 表示以只读的方式读取文件 if(ReadFile.fail())//文件打开失败:返回0 { return 0} else//文件存在 { while(!ReadFile.eof()) { ReadFile.getline(line,512,'\n')n++} return n}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)