从文件中读取malloc和realloc

从文件中读取malloc和realloc,c,malloc,realloc,C,Malloc,Realloc,我要做的就是读取文件内容并将其放入字符串中。 我在读取文件时遇到问题。我开始用fgets阅读,如果我的空间不够,我会重新分配内存。然而,当我再次尝试重新分配以缩小时,它崩溃了。我读到它可能与malloc的元数据有关,但我看不出我是如何影响它们的。提前谢谢 int readStringFromALE(int height,int width,char **stringImage,FILE *fout){ char buffer[BUFFER_SIZE] = {'\0'}; *st

我要做的就是读取文件内容并将其放入字符串中。
我在读取文件时遇到问题。我开始用fgets阅读,如果我的空间不够,我会重新分配内存。然而,当我再次尝试重新分配以缩小时,它崩溃了。我读到它可能与malloc的元数据有关,但我看不出我是如何影响它们的。提前谢谢

 int readStringFromALE(int height,int width,char **stringImage,FILE *fout){
    char buffer[BUFFER_SIZE] = {'\0'};
    *stringImage=(char *)malloc(sizeof(char)*BUFFER_SIZE+1);
    *stringImage[0]='\0';
    int i=1;
    int size=sizeof(char)*BUFFER_SIZE,readSize=0;
    while(fgets(buffer, sizeof(buffer), fout) != NULL){
        strncat(*stringImage,buffer,strlen(buffer)+1);
        readSize+=strlen(buffer)+1;
        printf("%s",buffer);
        if(size<=readSize){
            char *temp;
            temp=(char *)realloc(*stringImage,i*BUFFER_SIZE*sizeof(char)+1);
            if(temp==NULL){
                printf("Unable to allocate memory\n");
                return EXIT_FAILURE;
            }
            i++;
            *stringImage=temp;
            size=i*BUFFER_SIZE*sizeof(char);
        }
        if (buffer[strlen(buffer) - 2] == ':')
            break;
    }
    char *temp=(char *)realloc(*stringImage,strlen(*stringImage)+10);
    if(temp==NULL){
        printf("Unable to allocate memory\n");
        return EXIT_FAILURE;
    }
    *stringImage=temp;
    return EXIT_SUCCESS;
}
int readStringFromALE(int高度、int宽度、字符**stringImage、文件*fout){
字符缓冲区[缓冲区大小]={'\0'};
*stringImage=(char*)malloc(sizeof(char)*缓冲区大小+1);
*stringImage[0]='\0';
int i=1;
int size=sizeof(char)*缓冲区大小,readSize=0;
while(fgets(buffer,sizeof(buffer),fout)!=NULL){
strncat(*stringImage,buffer,strlen(buffer)+1);
readSize+=strlen(缓冲区)+1;
printf(“%s”,缓冲区);

如果(size尝试在每次调用时使用fgets读取字符串(*stringImage+len)的末尾

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 10

int readStringFromALE(int height,int width,char **stringImage,FILE *fout){
    char *temp = NULL;
    size_t size = 0;
    size_t dif = 0;
    size_t len = 0;

    while ( 1) {
        if ( NULL == ( temp = realloc ( *stringImage, size + BUFFER_SIZE))) {
            fprintf ( stderr, "problem realloc\n");
            free ( *stringImage);
            return 0;
        }
        *stringImage = temp;
        //len = strlen ( *stringImage);
        dif = BUFFER_SIZE + ( size - len);
        if ( fgets ( *stringImage + len, dif, fout)) {
            len += strlen ( *stringImage + len);
            if ((*stringImage)[strlen(*stringImage) - 2] == ':')
                break;
        }
        else {
            printf("\n\n---end of file---\n");
            break;
        }
        size += BUFFER_SIZE;
    }
    temp=(char *)realloc(*stringImage,strlen(*stringImage)+10);
    if(temp==NULL){
        printf("Unable to allocate memory\n");
        return EXIT_FAILURE;
    }
    *stringImage=temp;
    return EXIT_SUCCESS;
}

int main(int argc,char** argv){
    char *stringImage = NULL;//so realloc will act like malloc on first call
    int height = 0;
    int width = 0;
    FILE *fout = NULL;

    if ( NULL == ( fout = fopen ( "somefile.txt", "r"))) {
        perror ( "problem");
        return 0;
    }

    readStringFromALE( height, width, &stringImage, fout);
    fclose ( fout);
    printf("%s\n\n",stringImage);

    free ( stringImage);

    return 0;
}
#包括
#包括
#包括
#定义缓冲区大小10
int readStringFromALE(int高度、int宽度、字符**stringImage、文件*fout){
char*temp=NULL;
大小\u t大小=0;
尺寸dif=0;
尺寸长度=0;
而(1){
if(NULL==(temp=realloc(*stringImage,size+BUFFER_size))){
fprintf(标准,“问题真实位置”);
免费(*stringImage);
返回0;
}
*stringImage=温度;
//len=strlen(*stringImage);
dif=缓冲区大小+(大小-长度);
if(fgets(*stringImage+len、dif、fout)){
len+=strlen(*stringImage+len);
如果((*stringImage)[strlen(*stringImage)-2]===':')
打破
}
否则{
printf(“\n\n--文件结尾--\n”);
打破
}
大小+=缓冲区大小;
}
temp=(char*)realloc(*stringImage,strlen(*stringImage)+10;
if(temp==NULL){
printf(“无法分配内存\n”);
返回退出失败;
}
*stringImage=温度;
返回退出成功;
}
int main(int argc,字符**argv){
char*stringImage=NULL;//所以realloc将在第一次调用时像malloc一样工作
整数高度=0;
整数宽度=0;
FILE*fout=NULL;
if(NULL==(fout=fopen(“somefile.txt”、“r”)){
佩罗(“问题”);
返回0;
}
readStringFromALE(高度、宽度和stringImage、fout);
fclose(fout);
printf(“%s\n\n”,stringImage);
免费(stringImage);
返回0;
}

也许你想要
strncat(*stringImage,buffer,strlen(buffer)+1);
是危险的胡说八道。谢谢你,我已经试过了,效果很好!!!我从来没有想过用fgets来填充字符串。