簡體   English   中英

使用malloc和realloc從輸入文件到數組

[英]from input file to array using malloc and realloc

我正在嘗試從文件中讀取輸入,並使用malloc和realloc將每個字符串放入數組中。 因此,如果輸入文件是:

alex
john
jane
smith

數組的內容包含{“ alex \\ n”,“ john \\ n”,“ jane \\ n”,“ smith \\ n”}。 到目前為止,我已經做了類似的事情:

int n=0;
int size=1;
File *fp = fopen(args[0],"r");
int c;
char* inputFile;
inputFile = (char*) malloc(size);
if(fp==0){
  fprintf(stderr, "Cannot open file!\n");
  return -1;}
else{
  do{
    c = fgetc(fp);
    inputFile = (char*) realloc(inputFile, size+1);
    inputFile[n]=c;
    n++;
    size++;
  }while(c!=EOF);

我相信該算法將以這樣的數組結尾:{'a','l','e','x','\\ n','j','o','h','n',' \\ n','j','a','n','e','\\ n','s','m','i','t','h','\\ n'}

如何使inputFile成為2維數組? 我應該如何處理realloc?

您可能想先分配一個指針數組,其初始大小為10:

int size = 10;
char **inputFile= malloc(sizeof*inputFile*size);

然后,對於每個閱讀的單詞,您為其分配更多的內存,並將其插入數組:

char line[100];
fscanf(file, "%s", line);
inputFile[index++] = strdup(line);

現在檢查是否需要更多單詞,然后重新分配數組:

if (index==size) {
   size += 10;
   inputFile = realloc(inputFile, sizeof*inputFile*size);
}

因此,您最終會得到如下結果:

[0]->"alex"
[1]->"john"
[2]->"jane"
[3]->"smith"

完成后,您需要遍歷數組並釋放每個字符串,然后釋放數組,這部分留作練習:)

您可以嘗試以下操作,也可以研究如何完成。 它在我的linux機器上工作正常。 如果您有任何疑問,請告訴我。

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

const int INITIAL_MAX_LINES = 2;
const int MAX_LINES_INC = 2;

const int INITIAL_MAX_LINE_LENGTH = 2;
const int MAX_LINE_LENGTH_INC = 2;

int main (int argc, char *argv[])
{
  int nlines = 0, i;
  FILE *fp = fopen(argv[1], "r");
  char **inputFile, *buffer;
  int max_lines, c, buflen, bufpos, end_of_line;

  if (argc < 2) {
    printf("No enough arguments.\n");
    return -1;
  }

  max_lines = INITIAL_MAX_LINES;

  inputFile = (char **) malloc(max_lines * sizeof(char*));
  if (fp==0) {
    fprintf(stderr, "Cannot open file!\n");
    return -1;
  }
  else{
    /* Start with a buffer. */
    bufpos = 0;
    buflen = INITIAL_MAX_LINE_LENGTH;
    buffer = (char *) malloc(buflen * sizeof(char *));

    c = 0;
    while (c != EOF) {

      end_of_line = 0;

      c = fgetc(fp);

      if (c == EOF || c == '\n' || c == '\r') {
        end_of_line = 1;
       /* Discard this character. */
      }
      else {
        /* Put this character in the buffer. */
        /* But check if we have enough memory first! */
        /* Leave room for the null character at the end. */
        if (bufpos >= buflen - 1) {
          buflen += MAX_LINE_LENGTH_INC;
          buffer = (char *) realloc(buffer, buflen * sizeof(char));
        }
        buffer[bufpos] = c;
        bufpos++;
      }

      if (end_of_line) {
        /* Remember this line and get a new buffer. */
        /* Check if we need more memory. */
        if (nlines >= max_lines) {
          max_lines += MAX_LINES_INC;
          inputFile = (char **) realloc(inputFile, max_lines * sizeof(char*));
        }

        /* Null terminate the buffer.*/
        buffer[bufpos++] = 0;

        inputFile[nlines] = buffer;
        nlines++;

        bufpos = 0;
        buflen = INITIAL_MAX_LINE_LENGTH;
        buffer = (char *) malloc(buflen * sizeof(char *));
      }
    }
  }

  printf("%d lines\n", nlines);
  for (i=0; i<nlines; i++) {
    printf("%s\n", inputFile[i]);
  }
}

暫無
暫無

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

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