你说的
"一次读入不了",怎么个一次读入哦,瞅瞅你写的代码呗,2100,行不是一行行循环读入?
你的内存是如何申请的,是堆内存 struct ch *p[2100]然后再对每个指针分配空间,p[i]=(struct ch*)malloc(sizeof(struct ch)),还是struct ch *p=(struct ch*)malloc(2100*sizeof(struct ch))
,还是函吵陪行数栈内存,struct ch strarr[2100]
#include "stdio.h"
int main()
{
FILE *pf=NULL //文件指针
int filelen=0
int i=0
char *buf
pf=fopen("D:\\test.txt","r") //以只读方式打开文件
if(pf==NULL)
{
return 0
}
else
{
//获得文件长度
fseek(pf,0,SEEK_END) //文件指针移到末尾
filelen=ftell(pf) //获得文件当前指针位置,即为文件长度
rewind(pf) //将文清蔽件指针移到开头,准备读取
buf=malloc(filelen+1) //新滑槐建缓冲区,存储独处的数据
//将缓冲区的数据设置为0
for(i=0i<filelen+1i++)
buf[i]=0
//读取文件
fread(buf,filelen,1,pf) 答让州
//关闭文件
fclose(pf)
//buf中即为要读出的数据
printf("%s\n",buf) //输出一下数据,你可以随便怎么用
free(buf) //最后记得要释放
}
return 1
}
#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条)