大小为 50000,在 #DEFINE 中定义。这是工作代码。但是,如果我删除 8 乘以大小,那我应该可以工作,但不起作用。我遇到了这种情况 分段错误:11 为什么我应该分配比我需要的多 8 倍的内存?
您正在撰写有关此尺寸估算的文章:
char *string = (char*)calloc( (8*size+1), sizeof(char) );
但是使用的数组是int[],你将在磁盘中的每一行写入一个值
sprintf( &string[ strlen(string) ], "%d\n", arr[i] );
这似乎不必要的复杂。至于大小,假设所有值都是INT_MIN,a.k.a. (in limits.h)
#define INT_MIN (-2147483647 - 1)
为 4 字节整数。所以你有 11 个字符。只是。 10 位数字加一个信号符号。这将为您提供任何 int 值。为'\n'加1
但是……
-
为什么要使用 calloc()?
-
为什么不直接使用适合所有可能值的 size * 12-byte 数组?
-
为什么要声明一个新的char* 来保存char 格式的值,而不是一次只使用fprintf()?
-
为什么void 而不是仅仅返回-1 表示错误或返回成功时写入磁盘的iten 数量?
返回程序
如果您真的想在一次调用fputs() 时将数组写入磁盘,将整个巨大的字符串保存在内存中,请考虑sprintf() 返回写入的字节数,所以这是您需要的值用作指向输出字符串的指针...
如果您想使用内存分配,您可以分块进行。考虑如果所有值都低于 999,则 50.000 行每行不超过 4 个字节。但如果所有值都等于INT_MIN,则每行最多有 12 个字节。
因此,您可以使用sprintf() 的返回来更新指向字符串的指针,并在需要时使用realloc(),比如说,以几K 字节为单位分配。 (如果你真的想回信,我可以发布一个例子)
C 示例
下面的代码以您尝试的方式写入文件,并返回写入的总字节数。无论如何,这取决于数组的值。最大就是我说的,每行12个字节...
int fputsArray( unsigned size, int* array , const char* filename)
{
static char string[12 * MY_SIZE_ ] = {0};
unsigned ix = 0; // pointer to the next char to use in string
FILE* output = fopen( filename, "w");
if ( output == NULL ) return -1;
// file is open
for(int i = 0; i < size; i+= 1)
{
unsigned used = sprintf( (string + ix), "%d\n", array[i] );
ix += used;
}
fputs(string, output);
fclose(output);
return ix;
}
使用fprintf()
此代码使用fprintf() 编写相同的文件,而且更简单...
int fputsArray_b( unsigned size, int* array , const char* filename)
{
unsigned ix = 0; // bytes written
FILE* output = fopen( filename, "w");
if ( output == NULL ) return -1;
// file is open
for(int i = 0; i < size; i+= 1)
ix += fprintf( output, "%d\n", array[i]);
fclose(output);
return ix;
}
2个函数的完整测试
#define MY_SIZE_ 50000
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int fputsArray(const unsigned,int*,const char*);
int fputsArray_b(const unsigned,int*,const char*);
int main(void)
{
int value[MY_SIZE_];
srand(210726); // seed for today :)
value[0] = INT_MIN; // just to test: this is the longest value
for ( int i=1; i<MY_SIZE_; i+=1 ) value[i] = rand();
int used = fputsArray( MY_SIZE_, value, "test.txt");
printf("%d bytes written to disk\n", used );
used = fputsArray_b( MY_SIZE_, value, "test_b.txt");
printf("%d bytes written to disk using the alternate function\n", used );
return 0;
}
int fputsArray( unsigned size, int* array , const char* filename)
{
static char string[12 * MY_SIZE_ ] = {0};
unsigned ix = 0; // pointer to the next char to use in string
FILE* output = fopen( filename, "w");
if ( output == NULL ) return -1;
// file is open
for(int i = 0; i < size; i+= 1)
{
unsigned used = sprintf( (string + ix), "%d\n", array[i] );
ix += used;
}
fputs(string, output);
fclose(output);
return ix;
}
int fputsArray_b( unsigned size, int* array , const char* filename)
{
unsigned ix = 0; // bytes written
FILE* output = fopen( filename, "w");
if ( output == NULL ) return -1;
// file is open
for(int i = 0; i < size; i+= 1)
ix += fprintf( output, "%d\n", array[i]);
fclose(output);
return ix;
}
程序写入2个相同的文件...