1

アルファベット、CR、LF、'\0' を含むファイルを 1 行ずつ読み取る C コードを書いています。以下は私の添付コードサンプルです。配列内の行数がファイル内の行数と等しくなるように、各行のアルファベットのみを配列に格納し、列の長さを可変にする必要があります (i 番目の行の文字数によって異なります)。

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

int main()
{
    char *buffer[100];
    char temp[128];
    int c,i=0,j=0;
    int pos=0;
    FILE *file;
    file = fopen("input", "r");
    if (file) {
        while ((c = getc(file)) != EOF){
            if ((c>=65 && c<=90) || (c>=97 && c<=122))
                temp[pos++]=c;
            else if(pos>1) {
                temp[pos]='\0';
                buffer[i]=temp;
                printf ("%s\n",temp);
                i++;
                pos=0;
            }
        }
    }
    fclose(file);
    while (j<i){
        printf("%s\n",buffer[j]);
        j++;
    }
}

上記のコードを実行すると、すべての buffer[j] に同じ文字列が含まれます。コードの何が間違っていたのかを理解するのを手伝ってくれる人はいますか。

4

1 に答える 1