所提供的代码从文本文件中读取行,并将它们存储在动态分配的称为行的2D数组中(该模式总是在第32767行之后重复).然后,它连接特定的行(行[j]、行[k]、行[m]),并在输出文件中打印结果.问题是我的input.txt文件太大了,以至于我的PC无法处理这个-100 GB.因为使用Malloc时,我为2D数组分配了如此多的内存,以至于我的硬盘驱动器已满.有没有可能更改我的代码,这样我就不必分配这么多内存了?谢谢你的帮助!
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 300 // specifies the maximum length of a line in the input file
#define MAX_LINES 500000000 // specifies the maximum number of lines of the input file
int main()
{
// Open the file
FILE *fp = fopen("input.txt", "r");
if (fp == NULL)
{
printf("Failed to open the file.\n");
return 1;
}
// Allocate memory for lines
char **lines = (char **)malloc(MAX_LINES * sizeof(char *));
if (lines == NULL)
{
printf("Failed to allocate memory for lines.\n");
fclose(fp);
return 1;
}
for (int i = 0; i < MAX_LINES; i++)
{
lines[i] = (char *)malloc(MAX_LINE_LENGTH * sizeof(char));
if (lines[i] == NULL)
{
printf("Failed to allocate memory for line %d.\n", i);
// Free previously allocated lines
for (int j = 0; j < i; j++)
{
free(lines[j]);
}
free(lines);
fclose(fp);
return 1;
}
}
// Reset file pointer to the beginning
fseek(fp, 0, SEEK_SET);
// Read lines from the file and store them in lines array
int lineIndex = 0;
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), fp) != NULL)
{
// Remove newline character from the end of the line
if (line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = '\0';
strcpy(lines[lineIndex], line);
lineIndex++;
}
fclose(fp);
// Create output file
FILE *outputFile = fopen("output.txt", "w");
if (outputFile == NULL)
{
perror("Error creating output file");
// Free allocated lines
for (int i = 0; i < MAX_LINES; i++)
{
free(lines[i]);
}
free(lines);
return 1;
}
int i=1;
int j=5; //begin Permno number
int k=10927; //begin PRC date
int m=21849; //begin DLRETX data
int count=0;
int jj=10923; // end PERNMO data
int kk=21846; // end PRC data
int mm=32767; // end DLRETX data
int count_whileLoop=0;
// Append lines
for (i,j,k,m; j<=jj,k<=kk,m<=mm; i++,j++,k++,m++)
{
fprintf(outputFile, "%s%s%s\n", lines[j], lines[k], lines[m]);
}
// Free allocated memory
for (int i = 0; i < MAX_LINES; i++)
{
free(lines[i]);
}
free(lines);
// Close the output file
fclose(outputFile);
return 0;
}