簡體   English   中英

C-realloc()指針,然后在函數內部設置值

[英]C - realloc() pointer then set value inside function

我試圖用C編寫一個函數,該函數將文件讀入傳遞給它的char數組中。

void read_file(char* path, char** bufptr, char* buffer){
  /* open the file, and exit on errors */
  FILE *fp = fopen(path, "r");
  if(fp == NULL){
    perror("Failed to open file");
    exit(1);
  }
  /* figure out how long the file is to allocate that memory */
  fseek(fp, 0, SEEK_END);
  long length = ftell(fp);
  rewind(fp);
  /* allocate memory */
  *bufptr = realloc(*bufptr, length+1);
  fread(buffer, length, 1, fp);
  buffer[length] = 0;
}

我的想法是,我將使用這樣的東西:

int main(int argc, char** argv){
  char* somestring = malloc(1024);
  core_read_file("/some/file/path", &somestring, somestring);
  printf("In main(): %s\n", somestring);
  free(somestring);
  return 0;
}

但是,無論何時使用它,在程序編譯時,printf都不會向控制台輸出任何內容。 我剛開始並有點兒非常了解“間接”的概念,但是有人可以向我解釋為什么我的代碼無法按我期望的方式工作,以及我應該如何實現這一點功能。

(這不是家庭作業,因此任何可行的解決方案或方法都是完美的)

read_file的最后兩行應為:

  fread(*bufptr, length, 1, fp);
  (*bufptr)[length] = 0;

新分配的緩沖區中的指針位於*bufptr ,而不位於buffer

但是您的程序過於復雜,您不需要傳遞三個參數來做read_file 兩個就足夠了,像這樣:

void read_file(char* path, char** bufptr) {
  /* open the file, and exit on errors */
  FILE *fp = fopen(path, "r");
  if (fp == NULL) {
    perror("Failed to open file");
    exit(1);
  }
  /* figure out how long the file is to allocate that memory */
  fseek(fp, 0, SEEK_END);
  long length = ftell(fp);
  rewind(fp);
  /* allocate memory */
  *bufptr = realloc(*bufptr, length + 1);
  fread(*bufptr, length, 1, fp);
  (*bufptr)[length] = 0;
}    

int main(int argc, char** argv) {
  char* somestring = malloc(1024);  //  char* somestring = NULL would be even better here
  read_file("readme.txt", &somestring);
  printf("In main(): %s\n", somestring);
  free(somestring);
  return 0;
}

為了簡潔起見,這里仍然沒有錯誤檢查用於重新realloc

如果要在函數內部分配內存-則無需在外部分配內存。 但是應該有文件記錄的協議-在失敗的情況下-如果成功的話,任何東西都不能釋放-調用函數負責釋放緩沖區指針。

您的讀取功能必須進行更多的錯誤檢查,還必須關閉任何分支上的文件。

這是進行閱讀的示例代碼:

#define _CRT_SECURE_NO_WARNINGS

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

long read_file(const char* path, char** bufptr) {
    char* buffer;
    int res;
    size_t read;
    FILE *fp;
    if (path == NULL || bufptr == NULL) {
        perror("Invalid parameters");
        return -6;
    }
    /* open the file, and exit on errors */
    fp = fopen(path, "rb");
    if (fp == NULL) {
        perror("Failed to open file");
        return -1;
    }
    /* figure out how long the file is to allocate that memory */
    res = fseek(fp, 0, SEEK_END);
    if (res != 0) {
        perror("Failed to seek file");
        fclose(fp);
        return -2;
    }
    long length = ftell(fp);
    if (length <= 0) {
        perror("Failed ftell");
        fclose(fp);
        return -3;
    }
    rewind(fp);
    /* allocate memory */
    buffer = malloc(length + 1);
    if (buffer == NULL) {
        perror("Out of memory");
        fclose(fp);
        return -4;
    }
    read = fread(buffer, 1, length, fp);
    fclose(fp);

    if ((long)read != length) {
        perror("Failed to read whole file");
        free(buffer);
        return -5;
    }
    buffer[length] = 0;
    *bufptr = buffer;
    return length;
}
int main() {
    char* somestring;
    long res = read_file("c:/key.txt", &somestring);
    if (res < 0) {
        //nothing is leaked - everything opened or allocated was closed and freed by the function
        exit(res);
    }
    printf("In main(): %s\n", somestring);
    free(somestring);
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM