c - 从文件中使用 malloc 和 realloc 读取

标签 c malloc realloc

我想做的就是读取文件内容并将它们放入字符串中。
从文件读取时遇到问题。我开始使用 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;
}

最佳答案

尝试在每次调用时使用 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;
}

关于c - 从文件中使用 malloc 和 realloc 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47379514/

相关文章:

c - 如何 malloc modbus_t

c - 在 NULL 值(或未定义)指针上重新分配

c - 有多少种方法可以锁定c

c++ - 无符号整数如何工作

c - 使用指针和线程时避免竞争条件

c - 带有用户输入的二维数组

c - 打印字符串

c - 将结构写入二进制文件,然后读取文件并打印数据。

c - c中字符串数组的内存泄漏

c++ - 重新分配 C++ 数组的内存。