问题描述:
不能用vc
有一个700多mb的文件,总是刚州梁纳读了一点就读不下去,想问一下像这种文件怎么读取?
解析:
用内存映射方式,再就是循环一点一点读入到一个内存块中
#include <stdio.h>#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
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)