c++将矩阵数据保存为文本或 XML(更好)

发布于 2024-10-06 13:27:48 字数 868 浏览 2 评论 0原文

我创建了一个带有此函数的 file.dat

int writeData(double* v, int length, FILE* fh)
{
    FILE *Stream;     //Stream for output.
    double*Elem;     //Pointer to all the element to save.

    assert( fh!=NULL );
    Stream = fh;

    Elem=(double*)malloc((length+2)*sizeof(double));//Allocate memory to Elem.
    if(Elem==NULL)
    {
        printf("Can't allocate memory for saving the matrix!");
        return(0);
    }

    Elem[0]=(double)1;//Save the dimensions of the matrix.
    Elem[1]=(double)length;

    int i;
    for(i=0;i<length ;i++) 
        Elem[i+2]=v[i];

    if(fwrite((void*)Elem,sizeof(double),(length+2),Stream) < (unsigned)(length+2)) //Save the data.
    {
        printf("Error, can't save the matrix!");
        return(0);
    }
    free(Elem);
    return(1);
}

现在我想将此文件转换为 xml 文件或文本文件...

有什么提示吗? 谢谢

I created a file.dat whith this function

int writeData(double* v, int length, FILE* fh)
{
    FILE *Stream;     //Stream for output.
    double*Elem;     //Pointer to all the element to save.

    assert( fh!=NULL );
    Stream = fh;

    Elem=(double*)malloc((length+2)*sizeof(double));//Allocate memory to Elem.
    if(Elem==NULL)
    {
        printf("Can't allocate memory for saving the matrix!");
        return(0);
    }

    Elem[0]=(double)1;//Save the dimensions of the matrix.
    Elem[1]=(double)length;

    int i;
    for(i=0;i<length ;i++) 
        Elem[i+2]=v[i];

    if(fwrite((void*)Elem,sizeof(double),(length+2),Stream) < (unsigned)(length+2)) //Save the data.
    {
        printf("Error, can't save the matrix!");
        return(0);
    }
    free(Elem);
    return(1);
}

Now I'd like to convert this file into an xml one or into a text file...

Any tips?
Thanks

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

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

发布评论

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

评论(1

爱,才寂寞 2024-10-13 13:27:48

将值保存为文本格式很容易:

int writeTextData(double* v, int length, FILE* fh)
{
   assert( fh!=NULL );
   fprintf(fh, "%d\n", length);
   for(int i=0;i<length ;i++)
      if(fprintf(fh, "%lf ", v[i]) <= 0)
      {
         printf("Error, can't save the matrix!");
         return(0);
      }
   return(1);
}

将保存长度,然后保存长度数字。

XML 最好在库的帮助下编写。

虽然这样一个简单的数据集可以手动保存到 XML 中(通过 fprintf),但任何更复杂的数据集都需要您进行复杂的编码/转义,并且很容易出错,除非您精通 XML - 如前所述,您最好使用库(例如 libxml)

Saving the values into text format is easy:

int writeTextData(double* v, int length, FILE* fh)
{
   assert( fh!=NULL );
   fprintf(fh, "%d\n", length);
   for(int i=0;i<length ;i++)
      if(fprintf(fh, "%lf ", v[i]) <= 0)
      {
         printf("Error, can't save the matrix!");
         return(0);
      }
   return(1);
}

Will save length followed by length numbers.

XML is better written with a help of a library.

While such a simple dataset can be saved into XML manually (through fprintf's) anything more complicated will require you to do complex encoding/escaping and is very error prone, unless you are proficient with XML - as previously said, you'd better use a library (such as libxml)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文