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

int main()
{
    int i = 0, len1=0, len2=0;
    int BUFSIZE = 1000;
    char* string1[20];
    char* string2[20];
    FILE *fp1 = fopen("input1.txt", "r");
    FILE *fp2 = fopen("input2.txt", "r");
    if ((fp1 == 0)||(fp2 == 0)){
        fprintf(stderr, "Error while opening");
        return 0;
    }
    string1[i] = (char*)malloc(BUFSIZE);
    string2[i] = (char*)malloc(BUFSIZE);
    while (fgets(string1[i], BUFSIZE, fp1)) {
        i++;
        len1+=strlen(string[i]);
        string1[i] = (char*)malloc(BUFSIZE);
        len1+=strlen(string1[i]);
    }

    i = 0;
    while (fgets(string2[i], BUFSIZE, fp2)) {
        i++;
        string2[i] = (char*)malloc(BUFSIZE);
    }

    printf("Output: \n");
    srand(time(NULL));
    int j = rand()%i;
    int k = (j+1)%i;
    fflush(stdout);
    printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
    printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
    printf("\n");
    printf("%d", len1);

    int x;
    for(x = 0; x<i; x++){
        free(string1[x]);
        free(string2[x]);
    }
    scanf("%d", x);
    fclose(fp1);
    fclose(fp2);
    return 0;
}

user1807597 の助けのおかげで、2 つの文字列を読み取って配列に格納することにようやく気付きました。しかし、配列の長さを取得するのにまだ問題があります。len+=strlen(string[i]); を配置しようとしています。while ループ内で実行されますが、デバッグ時にコンパイラが中断します。誰かが理由を知っていますか?ありがとうございました!

4

2 に答える 2

4

配列の長さには、主に 2 つの選択肢があります。

  1. これ以上エントリを使用しないと思われるほど大きくします。

    enum { MAX_LINES = 16 * 1024 };
    char *lines[MAX_LINES];
    size_t num_lines = 0;
    
  2. 配列の動的割り当てを行います。

    char **lines = 0;
    size_t max_lines = 0;
    size_t num_lines = 0;
    
    ...
    
    if (num_lines >= max_lines)
    {
        size_t new_lines = max_lines * 2 + 2;
        char **space = realloc(lines, new_lines * sizeof(*space));
        if (space == 0)
            ...deal with out of memory...
        lines = space;
        max_lines = new_lines;
    }
    

両方のシステムが機能します。動的割り当ては、最初の数回は少し面倒ですが、メモリが不足するまで問題は発生しません。最近では、メモリが不足するまでに長い時間がかかります。固定割り当ては、間違っていると推測するとスペースが不足する可能性があり、小さなファイルのみを処理する場合は名目上メモリを無駄にします。「無駄な」メモリは、最近では通常非常に小さいです。

どちらが優れているかは、プログラムが処理すると予想される問題の規模によって異なります。それらが小さい場合は、固定されているが寛大な割り当てが賢明で簡単です。それらが大きくなる場合は、動的割り当てがより賢明です。

于 2012-11-10T02:39:57.293 に答える
1

コードを変更しました。今は問題ないと思います。最初のwhileループでカウントが間違っています。i++その後に来る必要がありますlen1+=strlen(string[i]);。最後のscanfステートメントscanf("%d", x);で、演算子「&」を見逃しています。

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

int main()
{
    int i = 0, len1 = 0, len2 = 0;
    int BUFSIZE = 1000;
    /*
       you have too  few lines here,
       If my file contains more than 20 lines,tragedy will occur!
       */
    char* string1[20];
    char* string2[20];

    FILE* fp1 = fopen("input1.txt", "r");
    FILE* fp2 = fopen("input2.txt", "r");

    if ((fp1 == 0) || (fp2 == 0))
    {
        fprintf(stderr, "Error while opening");
        return 0;
    }
    string1[i] = (char*)malloc(BUFSIZE);
    string2[i] = (char*)malloc(BUFSIZE);

    while (fgets(string1[i], BUFSIZE, fp1)!=NULL)
    {
        /*
        i++;
        */

        len1 += strlen(string1[i]);
        i++;
        string1[i] = (char*)malloc(BUFSIZE);
    }

    i = 0;
    while (fgets(string2[i], BUFSIZE, fp2))
    {
        i++;
        string2[i] = (char*)malloc(BUFSIZE);
    }

    printf("Output: \n");
    srand(time(NULL));
    int j = rand() % i;
    int k = (j + 1) % i;
    fflush(stdout);
    printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
    printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
    printf("\n");
    printf("%d", len1);

    int x;
    for (x = 0; x < i; x++)
    {
        free(string1[x]);
        free(string2[x]);
    }
    /*
    scanf("%d", x);
    */
    scanf("%d",&x);
    fclose(fp1);
    fclose(fp2);
    return 0;
}
于 2012-11-10T02:35:09.617 に答える