Issue with fwrite and fread in sequence





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying to write simple struct with an array to a file and then read it. It works with the array of a small size < 25 but for some reason all data after that is not initialized;



typedef struct TestStruct {
int data[30];
} TestStruct;



TestStruct *test = malloc(sizeof(TestStruct));
for (int i = 0; i < 30; i++)
{
test->data[i] = i;
}
const char *filename = "some.txt";
FILE *file = fopen(filename, "w+");
fwrite(test, sizeof(TestStruct), 1, file);
rewind(file);
TestStruct *test2 = malloc(sizeof(TestStruct));
int rc = fread(test2, sizeof(TestStruct), 1, file);


The result of this code is rc = 0 and integers after index 25 are not initialized for some reason. Can anybody explain where is the problem?










share|improve this question































    1















    I'm trying to write simple struct with an array to a file and then read it. It works with the array of a small size < 25 but for some reason all data after that is not initialized;



    typedef struct TestStruct {
    int data[30];
    } TestStruct;



    TestStruct *test = malloc(sizeof(TestStruct));
    for (int i = 0; i < 30; i++)
    {
    test->data[i] = i;
    }
    const char *filename = "some.txt";
    FILE *file = fopen(filename, "w+");
    fwrite(test, sizeof(TestStruct), 1, file);
    rewind(file);
    TestStruct *test2 = malloc(sizeof(TestStruct));
    int rc = fread(test2, sizeof(TestStruct), 1, file);


    The result of this code is rc = 0 and integers after index 25 are not initialized for some reason. Can anybody explain where is the problem?










    share|improve this question



























      1












      1








      1








      I'm trying to write simple struct with an array to a file and then read it. It works with the array of a small size < 25 but for some reason all data after that is not initialized;



      typedef struct TestStruct {
      int data[30];
      } TestStruct;



      TestStruct *test = malloc(sizeof(TestStruct));
      for (int i = 0; i < 30; i++)
      {
      test->data[i] = i;
      }
      const char *filename = "some.txt";
      FILE *file = fopen(filename, "w+");
      fwrite(test, sizeof(TestStruct), 1, file);
      rewind(file);
      TestStruct *test2 = malloc(sizeof(TestStruct));
      int rc = fread(test2, sizeof(TestStruct), 1, file);


      The result of this code is rc = 0 and integers after index 25 are not initialized for some reason. Can anybody explain where is the problem?










      share|improve this question
















      I'm trying to write simple struct with an array to a file and then read it. It works with the array of a small size < 25 but for some reason all data after that is not initialized;



      typedef struct TestStruct {
      int data[30];
      } TestStruct;



      TestStruct *test = malloc(sizeof(TestStruct));
      for (int i = 0; i < 30; i++)
      {
      test->data[i] = i;
      }
      const char *filename = "some.txt";
      FILE *file = fopen(filename, "w+");
      fwrite(test, sizeof(TestStruct), 1, file);
      rewind(file);
      TestStruct *test2 = malloc(sizeof(TestStruct));
      int rc = fread(test2, sizeof(TestStruct), 1, file);


      The result of this code is rc = 0 and integers after index 25 are not initialized for some reason. Can anybody explain where is the problem?







      c windows io






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 2:54









      Jonathan Leffler

      574k956881041




      574k956881041










      asked Jan 4 at 2:34









      Argus KosArgus Kos

      628




      628
























          1 Answer
          1






          active

          oldest

          votes


















          3















          • You're working on a Windows or DOS platform.

          • You're using binary data.

          • You write control-Z to the file (byte code 26 at index 26).

          • Because you didn't specify b (for binary) in the mode string used with fopen(), the control-Z is treated as an EOF marker when you read the data.


          Fix: Use "w+b" instead of just "w+" to deal with the problem.



          Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.



          You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.






          share|improve this answer



















          • 1





            Note also that the CRT does newline translation in text mode. Writing "n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".

            – eryksun
            Jan 4 at 4:05












          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%2f54032463%2fissue-with-fwrite-and-fread-in-sequence%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3















          • You're working on a Windows or DOS platform.

          • You're using binary data.

          • You write control-Z to the file (byte code 26 at index 26).

          • Because you didn't specify b (for binary) in the mode string used with fopen(), the control-Z is treated as an EOF marker when you read the data.


          Fix: Use "w+b" instead of just "w+" to deal with the problem.



          Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.



          You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.






          share|improve this answer



















          • 1





            Note also that the CRT does newline translation in text mode. Writing "n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".

            – eryksun
            Jan 4 at 4:05
















          3















          • You're working on a Windows or DOS platform.

          • You're using binary data.

          • You write control-Z to the file (byte code 26 at index 26).

          • Because you didn't specify b (for binary) in the mode string used with fopen(), the control-Z is treated as an EOF marker when you read the data.


          Fix: Use "w+b" instead of just "w+" to deal with the problem.



          Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.



          You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.






          share|improve this answer



















          • 1





            Note also that the CRT does newline translation in text mode. Writing "n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".

            – eryksun
            Jan 4 at 4:05














          3












          3








          3








          • You're working on a Windows or DOS platform.

          • You're using binary data.

          • You write control-Z to the file (byte code 26 at index 26).

          • Because you didn't specify b (for binary) in the mode string used with fopen(), the control-Z is treated as an EOF marker when you read the data.


          Fix: Use "w+b" instead of just "w+" to deal with the problem.



          Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.



          You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.






          share|improve this answer














          • You're working on a Windows or DOS platform.

          • You're using binary data.

          • You write control-Z to the file (byte code 26 at index 26).

          • Because you didn't specify b (for binary) in the mode string used with fopen(), the control-Z is treated as an EOF marker when you read the data.


          Fix: Use "w+b" instead of just "w+" to deal with the problem.



          Note that the return value rc = 0 from fread() means that the read failed to read the entire structure requested (because only 26 bytes, values 0..25, were read before EOF was detected). It did its best to let you know there was a problem.



          You should look at the return value from fwrite() too, to make sure that everything you expected to be written was in fact written.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 2:45









          Jonathan LefflerJonathan Leffler

          574k956881041




          574k956881041








          • 1





            Note also that the CRT does newline translation in text mode. Writing "n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".

            – eryksun
            Jan 4 at 4:05














          • 1





            Note also that the CRT does newline translation in text mode. Writing "n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".

            – eryksun
            Jan 4 at 4:05








          1




          1





          Note also that the CRT does newline translation in text mode. Writing "n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".

          – eryksun
          Jan 4 at 4:05





          Note also that the CRT does newline translation in text mode. Writing "n" gets converted to "rn" in the file, and reading "rn" gets converted back to "n".

          – eryksun
          Jan 4 at 4:05




















          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%2f54032463%2fissue-with-fwrite-and-fread-in-sequence%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

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'