0

In C++, how to implement the following functionality? Thanks. Assume in a program, I can get a matrix A = [1,2;2,1]. How to save it in an independent data file, e.g., data1.

Secondly, how to load this file data1 into my another program 2 as a matrix A.

2

1 Answer 1

1

Struct for C++ File I/O binary file sample

       struct WebSites
       {
             char SiteName[100];
             int Rank;
       };

to write

     void write_to_binary_file(WebSites p_Data)
     {
          fstream binary_file("test.dat",ios::out|ios::binary|ios::app);
          binary_file.write(reinterpret_cast<char *>(&p_Data),sizeof(WebSites));
     }

Sample for C++ File I/O binary file read

 void read_from_binary_file()
 {
     WebSites p_Data;
     fstream binary_file("test.dat",ios::binary|ios::in);
     binary_file.read(reinterpret_cast<char *>(&p_Data),sizeof(WebSites));
     binary_file.close();

     cout<<p_Data.SiteName<<endl;
     cout<<"Rank :"<< p_Data.Rank<<endl;
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.