1

ファイルから入力を読み取り、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で何をすべきですか?

4

2 に答える 2

0

最初に、初期サイズが 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"

完了したら、配列をループして各文字列を解放してから配列を解放する必要があります。この部分は演習として残します:)

于 2013-02-08T22:20:39.960 に答える
0

以下を試すか、それがどのように行われたかを調べることができます。私の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]);
  }
}
于 2013-02-08T23:24:16.790 に答える