Reading file of 1's and 0's one at a time









up vote
0
down vote

favorite












In C++ I am trying to read from the following file into an array:



0000000000
0000000000
0000000000
0001110000
0001010000
0001110000
0000000000
0000000000
0000000000
0000000000


I am using the following to put each int into an array:



 X = (int* )malloc(n*n*sizeof(int));
for (int i = 0; i<(n*n); i++)
j = read.get();
if (isdigit(j))
*(X+i) = j - '0';




But when I output to array to a file I get the following output:



0000000000
0000000000
0000000000
0000001110
0000000101
0000000011
1000000000
0000000000
0000000000
0000000000


And I don't believe there is anything wrong with how I am outputting my array:



for (int i = 0; i<(n*n); i++)
write << *(X+i);
if (((1+i) % n) == 0)
write << endl;




I tried reading with the following but it didn't seem to work:



for (int i = 0; i<(n*n); i++)

read >> *(X+i);










share|improve this question



















  • 2




    Don't use malloc() in C++, use new. Even better, use std::vector instead of C-style arrays.
    – Barmar
    Nov 9 at 23:38










  • It is not recommended to use malloc() in C++. You should generally use new or (better) a container like std::vector or std::array.
    – Galik
    Nov 9 at 23:38










  • The problem is in your read loop, on handling (or not handling) non-digits. The corresponding int in the buffer is left uninitialised, but the writing code assumes something is stored there. The result (accessing an uninitialised int in the write loop) is undefined behaviour.
    – Peter
    Nov 9 at 23:42















up vote
0
down vote

favorite












In C++ I am trying to read from the following file into an array:



0000000000
0000000000
0000000000
0001110000
0001010000
0001110000
0000000000
0000000000
0000000000
0000000000


I am using the following to put each int into an array:



 X = (int* )malloc(n*n*sizeof(int));
for (int i = 0; i<(n*n); i++)
j = read.get();
if (isdigit(j))
*(X+i) = j - '0';




But when I output to array to a file I get the following output:



0000000000
0000000000
0000000000
0000001110
0000000101
0000000011
1000000000
0000000000
0000000000
0000000000


And I don't believe there is anything wrong with how I am outputting my array:



for (int i = 0; i<(n*n); i++)
write << *(X+i);
if (((1+i) % n) == 0)
write << endl;




I tried reading with the following but it didn't seem to work:



for (int i = 0; i<(n*n); i++)

read >> *(X+i);










share|improve this question



















  • 2




    Don't use malloc() in C++, use new. Even better, use std::vector instead of C-style arrays.
    – Barmar
    Nov 9 at 23:38










  • It is not recommended to use malloc() in C++. You should generally use new or (better) a container like std::vector or std::array.
    – Galik
    Nov 9 at 23:38










  • The problem is in your read loop, on handling (or not handling) non-digits. The corresponding int in the buffer is left uninitialised, but the writing code assumes something is stored there. The result (accessing an uninitialised int in the write loop) is undefined behaviour.
    – Peter
    Nov 9 at 23:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In C++ I am trying to read from the following file into an array:



0000000000
0000000000
0000000000
0001110000
0001010000
0001110000
0000000000
0000000000
0000000000
0000000000


I am using the following to put each int into an array:



 X = (int* )malloc(n*n*sizeof(int));
for (int i = 0; i<(n*n); i++)
j = read.get();
if (isdigit(j))
*(X+i) = j - '0';




But when I output to array to a file I get the following output:



0000000000
0000000000
0000000000
0000001110
0000000101
0000000011
1000000000
0000000000
0000000000
0000000000


And I don't believe there is anything wrong with how I am outputting my array:



for (int i = 0; i<(n*n); i++)
write << *(X+i);
if (((1+i) % n) == 0)
write << endl;




I tried reading with the following but it didn't seem to work:



for (int i = 0; i<(n*n); i++)

read >> *(X+i);










share|improve this question















In C++ I am trying to read from the following file into an array:



0000000000
0000000000
0000000000
0001110000
0001010000
0001110000
0000000000
0000000000
0000000000
0000000000


I am using the following to put each int into an array:



 X = (int* )malloc(n*n*sizeof(int));
for (int i = 0; i<(n*n); i++)
j = read.get();
if (isdigit(j))
*(X+i) = j - '0';




But when I output to array to a file I get the following output:



0000000000
0000000000
0000000000
0000001110
0000000101
0000000011
1000000000
0000000000
0000000000
0000000000


And I don't believe there is anything wrong with how I am outputting my array:



for (int i = 0; i<(n*n); i++)
write << *(X+i);
if (((1+i) % n) == 0)
write << endl;




I tried reading with the following but it didn't seem to work:



for (int i = 0; i<(n*n); i++)

read >> *(X+i);







c++ file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 23:37









Galik

33.1k34674




33.1k34674










asked Nov 9 at 23:36









bob

91




91







  • 2




    Don't use malloc() in C++, use new. Even better, use std::vector instead of C-style arrays.
    – Barmar
    Nov 9 at 23:38










  • It is not recommended to use malloc() in C++. You should generally use new or (better) a container like std::vector or std::array.
    – Galik
    Nov 9 at 23:38










  • The problem is in your read loop, on handling (or not handling) non-digits. The corresponding int in the buffer is left uninitialised, but the writing code assumes something is stored there. The result (accessing an uninitialised int in the write loop) is undefined behaviour.
    – Peter
    Nov 9 at 23:42













  • 2




    Don't use malloc() in C++, use new. Even better, use std::vector instead of C-style arrays.
    – Barmar
    Nov 9 at 23:38










  • It is not recommended to use malloc() in C++. You should generally use new or (better) a container like std::vector or std::array.
    – Galik
    Nov 9 at 23:38










  • The problem is in your read loop, on handling (or not handling) non-digits. The corresponding int in the buffer is left uninitialised, but the writing code assumes something is stored there. The result (accessing an uninitialised int in the write loop) is undefined behaviour.
    – Peter
    Nov 9 at 23:42








2




2




Don't use malloc() in C++, use new. Even better, use std::vector instead of C-style arrays.
– Barmar
Nov 9 at 23:38




Don't use malloc() in C++, use new. Even better, use std::vector instead of C-style arrays.
– Barmar
Nov 9 at 23:38












It is not recommended to use malloc() in C++. You should generally use new or (better) a container like std::vector or std::array.
– Galik
Nov 9 at 23:38




It is not recommended to use malloc() in C++. You should generally use new or (better) a container like std::vector or std::array.
– Galik
Nov 9 at 23:38












The problem is in your read loop, on handling (or not handling) non-digits. The corresponding int in the buffer is left uninitialised, but the writing code assumes something is stored there. The result (accessing an uninitialised int in the write loop) is undefined behaviour.
– Peter
Nov 9 at 23:42





The problem is in your read loop, on handling (or not handling) non-digits. The corresponding int in the buffer is left uninitialised, but the writing code assumes something is stored there. The result (accessing an uninitialised int in the write loop) is undefined behaviour.
– Peter
Nov 9 at 23:42













1 Answer
1






active

oldest

votes

















up vote
4
down vote













The problem is that you're incrementing i even when isdigit(j) is false. So you're leaving the array elements corresponding to the newlines in the file uninitialized, and also not reading all the digits because you're counting the newlines. You need to put the increment inside the if.



for (int i = 0; i < n*n; ) 
char j = read.get();
if (isdigit(j))
X[i++] = j - '0';




BTW, if you're using a pointer as an array, use array syntax X[index] rather than *(X+index). It makes the intent clearer.






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',
    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%2f53234607%2freading-file-of-1s-and-0s-one-at-a-time%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








    up vote
    4
    down vote













    The problem is that you're incrementing i even when isdigit(j) is false. So you're leaving the array elements corresponding to the newlines in the file uninitialized, and also not reading all the digits because you're counting the newlines. You need to put the increment inside the if.



    for (int i = 0; i < n*n; ) 
    char j = read.get();
    if (isdigit(j))
    X[i++] = j - '0';




    BTW, if you're using a pointer as an array, use array syntax X[index] rather than *(X+index). It makes the intent clearer.






    share|improve this answer
























      up vote
      4
      down vote













      The problem is that you're incrementing i even when isdigit(j) is false. So you're leaving the array elements corresponding to the newlines in the file uninitialized, and also not reading all the digits because you're counting the newlines. You need to put the increment inside the if.



      for (int i = 0; i < n*n; ) 
      char j = read.get();
      if (isdigit(j))
      X[i++] = j - '0';




      BTW, if you're using a pointer as an array, use array syntax X[index] rather than *(X+index). It makes the intent clearer.






      share|improve this answer






















        up vote
        4
        down vote










        up vote
        4
        down vote









        The problem is that you're incrementing i even when isdigit(j) is false. So you're leaving the array elements corresponding to the newlines in the file uninitialized, and also not reading all the digits because you're counting the newlines. You need to put the increment inside the if.



        for (int i = 0; i < n*n; ) 
        char j = read.get();
        if (isdigit(j))
        X[i++] = j - '0';




        BTW, if you're using a pointer as an array, use array syntax X[index] rather than *(X+index). It makes the intent clearer.






        share|improve this answer












        The problem is that you're incrementing i even when isdigit(j) is false. So you're leaving the array elements corresponding to the newlines in the file uninitialized, and also not reading all the digits because you're counting the newlines. You need to put the increment inside the if.



        for (int i = 0; i < n*n; ) 
        char j = read.get();
        if (isdigit(j))
        X[i++] = j - '0';




        BTW, if you're using a pointer as an array, use array syntax X[index] rather than *(X+index). It makes the intent clearer.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 23:42









        Barmar

        413k34239339




        413k34239339



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234607%2freading-file-of-1s-and-0s-one-at-a-time%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo