简体   繁体   English

当我 fprintf 时,它写入了错误的变量

[英]When I fprintf, it writes the wrong variable

When I use fprintf , it actually saves the wrong content.当我使用fprintf时,它实际上保存了错误的内容。

Usually fprintf outputs what I want, but this time it's outputting seemingly irrelevant numbers, and I don't know what the problem is.平时fprintf输出的都是我想要的,但是这次输出的是看似无关紧要的数字,不知道是什么问题。

My Inputs & Expected output我的输入和预期 output

1 2 3
4 5 6
7 8 9

Actual Output实际Output

3 0 4199352 
1 11 0 
9 0 2 

I used a for loop to output a two-dimensional array我用了一个for循环到output一个二维数组

My code我的代码

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

void filein(int x, int y) {
    FILE *fin;
    int s[x][y];
    int i, j;
 
    if ((fin = fopen("test.txt", "r")) == NULL) {
        printf(" can't open");
    }
    else {
        printf("opening...\n");
    }

    for (i = 0; i < x; i++) {
        for (j = 0; j < y; j++) {
            fscanf(fin, "%d", &s[i][j]);
        }
  
        fscanf(fin, "\n");
    }
  
    fclose(fin);
  
    for (i = 0; i < x; i++) {
        for (j = 0; j < y; j++) {
            printf("%d ", s[i][j]);
            fflush(stdout);//输出
        }
      
        printf("\n");
    }
}

void fileout(int x, int y) {
    FILE *fout;
    int s[x][y];
    int i, j;
    char outname[50];
    printf("please input the name of output file (no include\".txt\"):\n");
    fflush(stdout);
    scanf("%s", outname);
    strcat(outname, ".txt\0");
    fout = fopen(outname,"w");
    
    if (fout == NULL) {
        printf("Error!");
        fflush(stdout);
    }
    else {
        printf("Writing....\n");
        fflush(stdout);
    }
    
    for (i = 0; i < x; i++) {
        for (j = 0; j < y; j++) {
            fprintf(fout, "%d ", s[i][j]);
        }

        fprintf(fout,"\n");
    }
    
    fclose(fout);
}

int main() {
    int x, y;
    printf("what is m?\n");
    fflush(stdout);
    scanf("%d", &x);
    printf("what is n?\n");
    fflush(stdout);
    scanf("%d", &y);
    filein(x, y);
    fileout(x, y);
    printf("finish");
    fflush(stdout);

    return 0;
}

The problem is that your fileout function doesn't have access to the data you read in your filein function. You read it into an array on the stack (the local variable s[x][y]), and then returned - that information is now gone.问题是您的fileout function 无法访问您在filein function 中读取的数据。您将其读入堆栈中的数组(局部变量 s[x][y]),然后返回 - 该信息现在不见了。 Then when fileout runs, the new s[x][y] is at some other place on the stack and you are just printing out whatever happens to be in memory there.然后当fileout运行时,新的 s[x][y] 位于堆栈中的某个其他位置,您只是打印出 memory 中的任何内容。

That's why your debugging printouts in filein show that you've read the data correctly, but the output is garbage.这就是为什么您在filein中的调试打印输出显示您已正确读取数据,但 output 是垃圾。

You'll need to malloc your space for the numbers in main and pass it around, or call a helper function between main and filein and fileout that allocates s on the stack as you've already done (but only once) and then passes s into the other functions.您需要在 malloc 中为main中的数字分配空间并传递它,或者在mainfileinfileout之间调用一个助手 function ,它会像您已经完成的那样在堆栈上分配s (但只有一次)然后传递s进入其他功能。

Your 2d array s in fileout function is allocated garbage numbers in the memory that's why you are getting different numbers from the numbers inside test.txt. fileout function 中的 2d array smemory中分配了垃圾编号,这就是为什么您得到的编号与 test.txt 中的编号不同的原因。

so I have solved that for you by defining a struct int_array2d_t* that has x and y of type size_t and array variables that could be accessed whenever needed.所以我通过定义一个struct int_array2d_t* 为您解决了这个问题,它具有 size_t 类型的 x 和 y 以及可以在需要时访问的数组变量。

The code that I added has comments that explain it, however, let me know if you do not understand sth!我添加的代码有解释它的注释,但是,如果您不明白,请告诉我!

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

/* define a struct that has x, y of type size_t and array */
typedef struct
{
  size_t x;
  size_t y;
  int array [];
} int_array2d_t;


/* initialize pointer of type struct int_array2d_t */
int_array2d_t* array2d = NULL;

void filein(int x, int y) {
    FILE *fin;
    int s[x][y];
    int i, j;
 
    if ((fin = fopen("test.txt", "r")) == NULL) {
        printf(" can't open");
    }
    else {
        printf("opening...\n");
    }

    for (i = 0; i < x; i++) {
        for (j = 0; j < y; j++) {
            fscanf(fin, "%d", &s[i][j]);
        }
  
        fscanf(fin, "\n");
    }
  
    fclose(fin);

    double count = 0.0;
    for (i = 0; i < x; i++) {
        for (j = 0; j < y; j++) {
            /* copy each value in s to array2d */
            array2d->array[i*y + j] = s[i][j];
            printf("%d ", array2d->array[i*y + j]);
            fflush(stdout);//输出
        }
      
        printf("\n");
    }
}

void fileout(int x, int y) {
    FILE *fout;
 
    int i, j;
    char outname[50];
    printf("please input the name of output file (no include\".txt\"):\n");
    fflush(stdout);
    scanf("%s", outname);
    strcat(outname, ".txt\0");
    fout = fopen(outname,"w");
    
    if (fout == NULL) {
        printf("Error!");
        fflush(stdout);
    }
    else {
        printf("Writing....\n");
        fflush(stdout);
    }
    
    for (i = 0; i < x; i++) {
        for (j = 0; j < y; j++) {
            /* write value in array2d->array to the file stream */
            fprintf(fout, "%d ", array2d->array[i*y + j]);
        }

        fprintf(fout,"\n");
    }
    
    fclose(fout);
}

int main() {
    int x, y;
    printf("what is m?\n");
    fflush(stdout);
    scanf("%d", &x);
    printf("what is n?\n");
    fflush(stdout);
    scanf("%d", &y);

    /* allocate array2d with the new size after we get x and y */
    array2d = malloc(sizeof(*array2d) + sizeof(double[x][y]));
    array2d->x = x;
    array2d->y = y;

    filein(x, y);
    fileout(x, y);
    printf("finish");
    fflush(stdout);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM