因此,我想做的是初始化一个无符号整数缓冲区,并使用文件指针(FILE * fp)从文件中读取并将文件内容存储在上述缓冲区中
问题是当我不使用malloc()并将缓冲区定义为无符号整数数组时,不会出现问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10000
void main()
{
unsigned int i = 0;
unsigned int *buffer = (unsigned int*)malloc(sizeof(unsigned int)*SIZE);
unsigned int c;
unsigned int length = 0;
FILE *fp = fopen("testfn.c", "r");
if (fp == NULL) {
printf("\nFile not found.\nExiting...");
exit(1);
}
while ((c = fgetc(fp)) != EOF)
{
buffer[i] = c;
++i;
}
length = i;
printf("\nNumber of elements= %d\n", length);
for (i = 0; i <= length; ++i)
printf("%c ", buffer[i]);
fclose(fp);
}
预期输出是文件内容,它们以整数形式存储并打印在屏幕上。
我收到的是分段错误,没有输出。