c读取大文件问题,求大神指点

c读取大文件问题,求大神指点,第1张

额,你听谁说C只能对65535个内存空间哦,65535才64K不到,怎么可能只有这点。只要内存装的下,现在主流的差不多2个G,加上虚拟内存2个G,减去乱伍系统用的,怎么也差不多将近2个G的内存给你用。类似现在的大型游戏,很吃内存的,这升哗个可能出现内存不够的问题。

你说的

"一次读入不了",怎么个一次读入哦,瞅瞅你写的代码呗,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

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8202279.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存