CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2013
    Posts
    4

    write to a binary file

    This is what I have so far

    My function does not work and my compiler says my int array is not initialized

    #include <iostream>
    #include <cctype>
    #include <string>
    #include <fstream>
    #include <stdio.h>

    using namespace std;

    void arrayToFile(ofstream tak, int *arr[], int size )
    {

    for (int i = 0; i<size; i++)
    {
    tak.write(&arr[i], sizeof(arr[i]));
    }
    }


    //void fileToArray(FILE got, int *arr[], int size1);

    int main()
    {
    int slen;
    slen = 2;
    int joly[slen] = { 1,2,3,4,5 };
    ofstream outfile;
    outfile.open("data.bin",ios:ut | ios::binary);
    arrayToFile(outfile, *joly, slen);



    return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: write to a binary file

    Before posting, please format your code and use code tags. Go Advanced, select the code and click '#'

    Code:
    int slen;
    slen = 2;
    int joly[slen] = { 1,2,3,4,5 };
    Array sizes are computed at compile time and not runtime. So array size needs to be a constant that the compiler can evaluate.

    Code:
    const int slen = 2;
    int joly[slen] = { 1,2 };
    This says that the array joly has 2 elements initialised to 1 and 2.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: write to a binary file

    Quote Originally Posted by laftaxxx View Post
    This is what I have so far

    My function does not work and my compiler says my int array is not initialized
    That is not the only problem.
    Code:
    void arrayToFile(ofstream tak,
    Streams are not copyable, therefore they cannot be passed by value as you're doing here. They must be passed by reference (or a pointer to the stream object must be passed).
    Code:
    void arrayToFile(ofstream& tak,
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Oct 2013
    Posts
    4

    Re: write to a binary file

    Thanks Paul I have made the corrections and still get errors.

    Corrected program

    #include <iostream>
    #include <cctype>
    #include <string>
    #include <fstream>
    #include <stdio.h>

    using namespace std;

    void arrayToFile(ofstream &tak, int *arr[], int size )
    {

    for (int i = 0; i<size; i++)
    {
    tak.write((char *)arr[i], sizeof(arr[i]));
    }
    }


    int main()
    {
    const int slen = 2;
    char joly[slen] = { 1,2, };
    ofstream outfile;
    outfile.open("data.bin",ios:ut | ios::binary);
    arrayToFile(&outfile, *joly, slen);



    return 0;
    }


    error message

    C:\Users\Public\Documents\lkh\main.cpp||In function 'int main()':|
    C:\Users\Public\Documents\lkh\main.cpp|25|error: invalid initialization of non-const reference of type 'std:fstream& {aka std::basic_ofstream<char>&}' from an rvalue of type 'std:fstream* {aka std::basic_ofstream<char>*}'|
    C:\Users\Public\Documents\lkh\main.cpp|9|error: in passing argument 1 of 'void arrayToFile(std:fstream&, int**, int)'|
    ||=== Build finished: 2 errors, 0 warnings (0 minutes, 3 seconds) ===|

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: write to a binary file

    Since the first parameter of arrayToFile is of type reference to ofstream, you should not pass a pointer as an argument, i.e., pass outfile, not &outfile.

    By the way, please post code in [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Oct 2013
    Posts
    4

    Re: write to a binary file

    thanks laserlight. I passed the outfile, got the following error.

    C:\Users\Public\Documents\lkh\main.cpp||In function 'int main()':|
    C:\Users\Public\Documents\lkh\main.cpp|25|error: invalid conversion from 'char' to 'int**' [-fpermissive]|
    C:\Users\Public\Documents\lkh\main.cpp|9|error: initializing argument 2 of 'void arrayToFile(std:fstream&, int**, int)' [-fpermissive]|
    ||=== Build finished: 2 errors, 0 warnings (0 minutes, 5 seconds) ===|

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: write to a binary file

    Quote Originally Posted by laftaxxx View Post
    thanks laserlight. I passed the outfile, got the following error.

    C:\Users\Public\Documents\lkh\main.cpp||In function 'int main()':|
    C:\Users\Public\Documents\lkh\main.cpp|25|error: invalid conversion from 'char' to 'int**' [-fpermissive]|
    C:\Users\Public\Documents\lkh\main.cpp|9|error: initializing argument 2 of 'void arrayToFile(std:fstream&, int**, int)' [-fpermissive]|
    ||=== Build finished: 2 errors, 0 warnings (0 minutes, 5 seconds) ===|
    The data type that's expected is int, but you changed your array type to char.

    But before going on, where did you get this code from, or are you trying to write something yourself "off the cuff"?

    I ask this, since all the code I know of that teaches you passing arrays as parameters do not show doing what you're doing. If you're passing a one dimensional array, you pass the array name only. The receiving function then would have a prototype as the following:
    Code:
    void arrayToFile(std::ofstream&, int*, int)
    or
    Code:
    void arrayToFile(std::ofstream&, int[], int)
    Both of these prototypes are exactly the same. Arrays decay to pointers when you pass the name of the array. In reality, when you "pass arrays", the address of the first item in the array is actually passed.

    Regards,

    Paul McKenzie

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: write to a binary file

    Your arrayToFile function also has a problem with the write.

    Code:
    void arrayToFile(ofstream &tak, int arr[], int size )
    {
    	for (int i = 0; i<size; i++)
    	{
    		tak.write((char *)&arr[i], sizeof(arr[i]));
    	}
    }
    write() requires a pointer to the memory but arr[i] returns the actual value, not the memory address. You need &arr[i] to obtain the required address.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Oct 2013
    Posts
    4

    Re: write to a binary file

    Thanks Paul

    So my function should be

    void arrayToFile(const char * filename, int*array, int size)

    sorry about the char instead of int...oversight.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: write to a binary file

    Have you read post #8?????
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured