Dumping a INT32 array into a .bin file












2















I have the array defined as below



INT32 LUT_OffsetValues[6][12] = {
0,180,360,540,720,900,1080,1260,1440,1620,1800,1980,
2160,2340,2520,2700,2880,3060,3240,3420,3600,3780,3960,4140,
4320,4500,4680,4860,5040,5220,5400,5580,5760,5940,6120,6300,
6480,6660,6840,7020,7200,7380,7560,7740,7920,8100,8280,8460,
8640,8820,9000,9180,9360,9540,9720,9900,10080,10260,10440,10620,
10800,10980,11160,11340,11520,11700,11880,12060,12240,12420,12600,12780
};

int main(int argc,char *argv)
{
int var_row_index = 4 ;
int var_column_index = 5 ;
int computed_val = 0 ;
FILE *fp = NULL ;

fp = fopen("./LUT_Offset.bin","wb");
if(NULL != fp)
{
fwrite(LUT_OffsetValues,sizeof(INT32),72,fp);
fclose(fp);
}

printf("Size of Array:%dn",sizeof(LUT_OffsetValues));
//computed_val = LUT_OffsetValues[var_row_index][var_column_index];
return 0;
}


Above is the code snippet with which I have generated the .bin file. Is that the right way of doing it?










share|improve this question



























    2















    I have the array defined as below



    INT32 LUT_OffsetValues[6][12] = {
    0,180,360,540,720,900,1080,1260,1440,1620,1800,1980,
    2160,2340,2520,2700,2880,3060,3240,3420,3600,3780,3960,4140,
    4320,4500,4680,4860,5040,5220,5400,5580,5760,5940,6120,6300,
    6480,6660,6840,7020,7200,7380,7560,7740,7920,8100,8280,8460,
    8640,8820,9000,9180,9360,9540,9720,9900,10080,10260,10440,10620,
    10800,10980,11160,11340,11520,11700,11880,12060,12240,12420,12600,12780
    };

    int main(int argc,char *argv)
    {
    int var_row_index = 4 ;
    int var_column_index = 5 ;
    int computed_val = 0 ;
    FILE *fp = NULL ;

    fp = fopen("./LUT_Offset.bin","wb");
    if(NULL != fp)
    {
    fwrite(LUT_OffsetValues,sizeof(INT32),72,fp);
    fclose(fp);
    }

    printf("Size of Array:%dn",sizeof(LUT_OffsetValues));
    //computed_val = LUT_OffsetValues[var_row_index][var_column_index];
    return 0;
    }


    Above is the code snippet with which I have generated the .bin file. Is that the right way of doing it?










    share|improve this question

























      2












      2








      2








      I have the array defined as below



      INT32 LUT_OffsetValues[6][12] = {
      0,180,360,540,720,900,1080,1260,1440,1620,1800,1980,
      2160,2340,2520,2700,2880,3060,3240,3420,3600,3780,3960,4140,
      4320,4500,4680,4860,5040,5220,5400,5580,5760,5940,6120,6300,
      6480,6660,6840,7020,7200,7380,7560,7740,7920,8100,8280,8460,
      8640,8820,9000,9180,9360,9540,9720,9900,10080,10260,10440,10620,
      10800,10980,11160,11340,11520,11700,11880,12060,12240,12420,12600,12780
      };

      int main(int argc,char *argv)
      {
      int var_row_index = 4 ;
      int var_column_index = 5 ;
      int computed_val = 0 ;
      FILE *fp = NULL ;

      fp = fopen("./LUT_Offset.bin","wb");
      if(NULL != fp)
      {
      fwrite(LUT_OffsetValues,sizeof(INT32),72,fp);
      fclose(fp);
      }

      printf("Size of Array:%dn",sizeof(LUT_OffsetValues));
      //computed_val = LUT_OffsetValues[var_row_index][var_column_index];
      return 0;
      }


      Above is the code snippet with which I have generated the .bin file. Is that the right way of doing it?










      share|improve this question














      I have the array defined as below



      INT32 LUT_OffsetValues[6][12] = {
      0,180,360,540,720,900,1080,1260,1440,1620,1800,1980,
      2160,2340,2520,2700,2880,3060,3240,3420,3600,3780,3960,4140,
      4320,4500,4680,4860,5040,5220,5400,5580,5760,5940,6120,6300,
      6480,6660,6840,7020,7200,7380,7560,7740,7920,8100,8280,8460,
      8640,8820,9000,9180,9360,9540,9720,9900,10080,10260,10440,10620,
      10800,10980,11160,11340,11520,11700,11880,12060,12240,12420,12600,12780
      };

      int main(int argc,char *argv)
      {
      int var_row_index = 4 ;
      int var_column_index = 5 ;
      int computed_val = 0 ;
      FILE *fp = NULL ;

      fp = fopen("./LUT_Offset.bin","wb");
      if(NULL != fp)
      {
      fwrite(LUT_OffsetValues,sizeof(INT32),72,fp);
      fclose(fp);
      }

      printf("Size of Array:%dn",sizeof(LUT_OffsetValues));
      //computed_val = LUT_OffsetValues[var_row_index][var_column_index];
      return 0;
      }


      Above is the code snippet with which I have generated the .bin file. Is that the right way of doing it?







      c






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 8:42









      Archana CharatiArchana Charati

      111




      111
























          3 Answers
          3






          active

          oldest

          votes


















          1














          No, it is not the right way if you plan to transfer the file to a different machine and read it as you haven't considered the Endianness. Let's say the file is:




          1. Written in little endian machine but read in big endian machine

          2. Written in big endian machine but read in little endian machine


          It won't work for none of the cases above.






          share|improve this answer































            0














            Out of the order of the bytes signaled by askinoor, that way is not generic because the reader have to now it is an INT32[6][12] when it read it



            Why the useless variables var_row_index etc in your program ?






            share|improve this answer































              0














              As already mentioned, when serializing data in and out of the CPU it is preferable to force network byte order. This can be done easily using functions like htonl(), which should be available on most platforms (and compile down to nothing on big endian machines).



              Here's the doc from Linux:
              https://linux.die.net/man/3/htonl



              Also, it's not good practice to explicitly code sizes and types into your program.
              Use sizeof(array[0][0]) to get the size of the element type of array, then iterate over it and use htonl() to write each element to the file.






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994115%2fdumping-a-int32-array-into-a-bin-file%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                No, it is not the right way if you plan to transfer the file to a different machine and read it as you haven't considered the Endianness. Let's say the file is:




                1. Written in little endian machine but read in big endian machine

                2. Written in big endian machine but read in little endian machine


                It won't work for none of the cases above.






                share|improve this answer




























                  1














                  No, it is not the right way if you plan to transfer the file to a different machine and read it as you haven't considered the Endianness. Let's say the file is:




                  1. Written in little endian machine but read in big endian machine

                  2. Written in big endian machine but read in little endian machine


                  It won't work for none of the cases above.






                  share|improve this answer


























                    1












                    1








                    1







                    No, it is not the right way if you plan to transfer the file to a different machine and read it as you haven't considered the Endianness. Let's say the file is:




                    1. Written in little endian machine but read in big endian machine

                    2. Written in big endian machine but read in little endian machine


                    It won't work for none of the cases above.






                    share|improve this answer













                    No, it is not the right way if you plan to transfer the file to a different machine and read it as you haven't considered the Endianness. Let's say the file is:




                    1. Written in little endian machine but read in big endian machine

                    2. Written in big endian machine but read in little endian machine


                    It won't work for none of the cases above.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 1 at 8:50









                    taskinoortaskinoor

                    39.9k7101128




                    39.9k7101128

























                        0














                        Out of the order of the bytes signaled by askinoor, that way is not generic because the reader have to now it is an INT32[6][12] when it read it



                        Why the useless variables var_row_index etc in your program ?






                        share|improve this answer




























                          0














                          Out of the order of the bytes signaled by askinoor, that way is not generic because the reader have to now it is an INT32[6][12] when it read it



                          Why the useless variables var_row_index etc in your program ?






                          share|improve this answer


























                            0












                            0








                            0







                            Out of the order of the bytes signaled by askinoor, that way is not generic because the reader have to now it is an INT32[6][12] when it read it



                            Why the useless variables var_row_index etc in your program ?






                            share|improve this answer













                            Out of the order of the bytes signaled by askinoor, that way is not generic because the reader have to now it is an INT32[6][12] when it read it



                            Why the useless variables var_row_index etc in your program ?







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 1 at 8:56









                            brunobruno

                            7,64311024




                            7,64311024























                                0














                                As already mentioned, when serializing data in and out of the CPU it is preferable to force network byte order. This can be done easily using functions like htonl(), which should be available on most platforms (and compile down to nothing on big endian machines).



                                Here's the doc from Linux:
                                https://linux.die.net/man/3/htonl



                                Also, it's not good practice to explicitly code sizes and types into your program.
                                Use sizeof(array[0][0]) to get the size of the element type of array, then iterate over it and use htonl() to write each element to the file.






                                share|improve this answer




























                                  0














                                  As already mentioned, when serializing data in and out of the CPU it is preferable to force network byte order. This can be done easily using functions like htonl(), which should be available on most platforms (and compile down to nothing on big endian machines).



                                  Here's the doc from Linux:
                                  https://linux.die.net/man/3/htonl



                                  Also, it's not good practice to explicitly code sizes and types into your program.
                                  Use sizeof(array[0][0]) to get the size of the element type of array, then iterate over it and use htonl() to write each element to the file.






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    As already mentioned, when serializing data in and out of the CPU it is preferable to force network byte order. This can be done easily using functions like htonl(), which should be available on most platforms (and compile down to nothing on big endian machines).



                                    Here's the doc from Linux:
                                    https://linux.die.net/man/3/htonl



                                    Also, it's not good practice to explicitly code sizes and types into your program.
                                    Use sizeof(array[0][0]) to get the size of the element type of array, then iterate over it and use htonl() to write each element to the file.






                                    share|improve this answer













                                    As already mentioned, when serializing data in and out of the CPU it is preferable to force network byte order. This can be done easily using functions like htonl(), which should be available on most platforms (and compile down to nothing on big endian machines).



                                    Here's the doc from Linux:
                                    https://linux.die.net/man/3/htonl



                                    Also, it's not good practice to explicitly code sizes and types into your program.
                                    Use sizeof(array[0][0]) to get the size of the element type of array, then iterate over it and use htonl() to write each element to the file.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 3 at 17:59









                                    idobyidoby

                                    785919




                                    785919






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994115%2fdumping-a-int32-array-into-a-bin-file%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Monofisismo

                                        Angular Downloading a file using contenturl with Basic Authentication

                                        Olmecas