簡體   English   中英

將值數組從一個 function 移動到另一個 C

[英]Moving array of values from one function to another in C

我正在開發一個程序,它從 .dat 文件中讀取 5x5 矩陣,執行少量任務並將這些任務的輸出保存到單獨的文件中。 目前我有兩個功能。 一種用於從文件中讀取並在控制台中顯示值。 還有一個用於將值保存在文件中。

我的問題是,當我將它們放在 main 中時,這兩個工作正常,但是當我將它們作為函數調用時,只有 read_matrix() 正確,而 write_matrix() 只會創建空白的 Result.dat。 我想這是因為當它們都在 main 中時,在讀取矩陣后,所有值都在 memory 中並且可以寫入文件。 當我將它們稱為函數時, write_matrix 在 A[] 中沒有任何值,並且什么也沒給我。 我嘗試創建另一個數組來保存這些值,弄亂指針(我對它們不是很熟悉),但沒有任何效果,而且我沒有想法。

  1. 我如何解決它? 如何將整個值數組從第一個 function 傳遞給另一個?

  2. 我怎樣才能讓程序確定尺寸。 我必須使用的矩陣是 5x5,但我希望能夠將此程序與其他尺寸一起使用。 目前我提示用戶輸入高度和寬度,但它遠非完美,如果我輸入不正確的尺寸 output 是錯誤的。

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


int read_matrix(FILE * fin, int ** A, int x, int y) {
  fin = fopen("mat_A.dat", "r");
  if (fin == NULL)
    printf("Error opening input file.  \n");

  else {
    printf("Number of columns:\n");
    scanf("%d", & x);
    printf("Number of rows:\n");
    scanf("%d", & y);
    // Read the matrix dimensions
    int ** A = (int ** ) malloc(sizeof(int * ) * x);
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        A[i] = (int * ) malloc(sizeof(int) * y);
      }
    }

    // Allocate the memory
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        fscanf(fin, "%d", & A[i][j]);
        printf("%d   ", A[i][j]);
      }
      printf("\n");
    }

    fclose(fin);
  }
}

int write_matrix(FILE * fout, int ** A, int x, int y) {
  fout = fopen("Result.dat", "w");
  // fprintf( fout, "%d %d \n", x, y );

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      fprintf(fout, "%d ", A[i][j]);
    }
    fprintf(fout, "\n");
  }
  fclose(fout);
}

int main() {
  /*  declaration of variables  */
  int x,
      y,
      i = 0, 
      **A;
      FILE * fin,
           * fout;

  read_matrix(fin, A, x, y);

  write_matrix(fout, A, x, y);

  printf("\n\nlul");
  free(A);
  return 0;
}```

我找到了一個返回 Result.txt 的解決方案,其中包含值。 在我的原始代碼中,我不僅沒有給write_matrix()任何值,而且我也沒有給它任何維度。 這是我為獲取帶有矩陣的文本文件所做的工作。

  1. 在第一個 function 我添加了返回 A; 在最后。
  2. 我將詢問用戶的尺寸從第一個 function 移到 main。 這樣他們就不會在第一個 function 結束后丟失。
  3. 在 main 中,我創建了另一個 int **B 指針。
  4. 我在調用第一個 function 時使用了該指針,然后在調用第二個 function 時將其用作參數。

以下是write_matrix()main()在更改后的處理方式。

int **read_matrix(int x, int y)
{
    int ** A = NULL;
    FILE * fin = fopen("mat_A.dat", "r");

    ...

        fclose(fin);
    }
    return A;
}

void write_matrix (int **B, int x, int y)
{
    FILE *fout = fopen( "Result.txt", "w" );

    for( int i = 0; i < x; i++ )
    {
        for( int j = 0; j < y; j++ )
        {
            fprintf( fout, "%d ", B[ i ][ j ] );
        }
        fprintf( fout, "\n" );
    }
    fclose( fout );
}

int main(void)
{
    /*  deklaracja i inicjalizacja zmiennych  */
    int x, y;
    int **B;

    printf("Number of columns:\n");
    scanf("%d", &x );
    printf("Number of rows:\n");
    scanf("%d", &y );
    printf("\n");

    B = read_matrix (x, y);

    if (B != NULL)
        write_matrix (B, x, y);

    printf("\n\nlul");
    free(B);
    return 0;
}

暫無
暫無

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

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