Linux C 打印变量数组内容到一个文件中的方法。

思路:

  1、以追加的方式新建打开一个文件

  2、将buf 按字节,先将整型转换为字符串,之后写入到文件中。

     

    memset(tmp_buf, 0, sizeof(tmp_buf));
    sprintf(tmp_buf, "./debug%d.txt", frame);
    test_fd = open(tmp_buf, O_CREAT | O_RDWR | O_NONBLOCK | O_TRUNC, 0777);
    if(test_fd < 0)
    {
        printf("open error\n");
        return - 1;
    }
    sprintf(tmp_buf, "%d-------------", frame);
    Write(test_fd, tmp_buf , strlen(tmp_buf));
    for(int i = 0; i < sizeof(buf); i++)
    {
        len = itoa(m.bmp32ff[i],tmp_buf, 10);
        Write(test_fd, tmp_buf, len);
        Write(test_fd," ", 1);
    }
    sprintf(tmp_buf, "-------------end\n");
    Write(test_fd, tmp_buf , strlen(tmp_buf));

 

  

posted @ 2022-05-24 15:53  大龄小凡  阅读(312)  评论(0编辑  收藏  举报