将ints的矩阵写入.txt

发布于 2025-01-26 20:05:38 字数 329 浏览 1 评论 0原文

我正在尝试将int s的479x639矩阵写入.txt文件。最好每行包括一个条目,然后包括,以便我可以在MATLAB上输入数据。我使用以下代码尝试将原始数据写入.txt

FILE *f = fopen("output.txt", "w");
fwrite(output, sizeof(int), 479*639, f);    
fclose(f);

我所得到的只是带有损坏数据的txt。该程序的其余部分运行顺利。有什么建议吗?

I'm trying to write a 479x639 matrix of ints to a .txt file. Preferably each line will include one entry followed by a ,, so that I can input the data on MATLAB. I used the following code to try and write the raw data to a .txt:

FILE *f = fopen("output.txt", "w");
fwrite(output, sizeof(int), 479*639, f);    
fclose(f);

All I get is a txt with corrupted data. The rest of the program runs smoothly. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2025-02-02 20:05:38

尝试一下,告诉我它是否有效

#include <stdio.h>

int main() {
    int output[479][639];

    // Add values into output 2D matrix

    FILE *f = fopen("output.txt", "w");
    for (int i = 0; i < 479; i++) {
        for (int j = 0; j < 639; j++) {
            fprintf(f, "%d, ", output[i][j]);  
        }
        fprintf(f, "\n");
    }  
    fclose(f);
    
    return 0;
}

Try this out and tell me if it works

#include <stdio.h>

int main() {
    int output[479][639];

    // Add values into output 2D matrix

    FILE *f = fopen("output.txt", "w");
    for (int i = 0; i < 479; i++) {
        for (int j = 0; j < 639; j++) {
            fprintf(f, "%d, ", output[i][j]);  
        }
        fprintf(f, "\n");
    }  
    fclose(f);
    
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文