以下是引用心空之上在2018-9-26 20:26:46的发言:
是这样声明吗?
char * tempstring[50000];
int i;
for(i = 0; i< 50000;i++)
{
tempstring = (char*)malloc(256);
}
这是静态分配空间,在栈区的话很有限
可以试试放在堆区
或动态分配,例如:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 256
main()
{
//测试文件数据
FILE *fp = fopen("test.txt","w+");
fprintf(fp, "%s\n", "abcd");
fprintf(fp, "%s\n", "12345");
fprintf(fp, "%s\n", "");
fprintf(fp, "%s\n", "ABCDEF");
fseek(fp, 0, SEEK_SET);
//从文件读入内存
char buf[BUFFER_SIZE];
char *p=buf;
char **pp=NULL;
int i, len, n=0;
while((fgets(buf,BUFFER_SIZE,fp)) != NULL)
{
if ((p=strchr(buf,'\n')) != NULL)
*p = '\0';
len = strlen(buf)+1;
if (len > 1)
{
pp = (char**)realloc(pp, (n+1)*sizeof(char*));
pp[n] = (char*)malloc(len);
strcpy(pp[n], buf);
++n;
}
}
fclose(fp);
for (i=0; i<n; ++i)
printf("%s\n", pp[i]);
for (i=0; i<n; ++i)
free(pp[i]);
free(pp);
}
[此贴子已经被作者于2018-9-27 09:29编辑过]