-1

I have built this code to output a binary file containing a look up table for a multiplexed 7 segment display.

I am pretty new to coding but am learning. I think I am pretty close, but I am not sure what I am missing to make this code run properly.

I get this error for each loop:

error: no match for call to '(std::ofstream {aka std::basic_ofstream<char>}) (uint32_t, int8_t&)

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

uint32_t value = 0;

int main()
{
    //Program data bytes

    cout << "Writing Binary File2";

    int8_t digits[] = {0X3f, 0X06, 0X5b, 0X4f, 0X66, 0X6d, 0X7d, 0X07, 0X7f, 0X6f, 0X77, 0X7c, 0X39, 0X5e, 0X79, 0X71 }; // 7-segment display output bytes for hexidecimal


    std::ofstream fout1("HexDisplay.bin", fout1.binary|fout1.out);

    if (!fout1) {
        std::cerr << "Error opening output files.\n";
        return 1;
    }

    for (value = 0; value <= 65535; value +=1){
        fout1(value + 0, digits[(value / 1) % 16]);
    }
    cout <<("OnesDone!..."); //

    for (value = 0; value <= 65535; value +=1){
        fout1(value + 65536, digits[(value / 16) % 16]);
    }
    cout <<("TensDone!...");

    for (value = 0; value <= 65535; value +=1){
         fout1(value + 131072, digits[(value / 256) % 16]);
    }
    cout <<("HundredsDone!...");

    for (value = 0; value <= 65535; value +=1){
        fout1(value + 196608, digits[(value / 4096) % 16]);
    }
    cout <<("ThousandsDone!...");

    fout1.write((char*)&fout1, 1);

    cout << "Programming Complete!...";
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Cramano
  • 9
  • 3
  • `fout1(value + 0, digits[(value / 1) % 16]);` -- can you briefly quote the snippet from your C++ textbook that led you to believe that this is the correct way to use a `std::ofstream` to accomplish what you want to do here? – Sam Varshavchik Dec 26 '22 at 22:54
  • the coding I know I learned from Arduino projects and many, many online tutorials, that code is mostly from an arduino eeprom programmer project, and a small bit of code I use to output another binary file. i have attempted to interpret and transpose them to this layout – Cramano Dec 26 '22 at 23:20
  • This is not how `std::ostream` gets used, no tutorial would actually show something of this nature, you must be misunderstanding or misreading something. It is unclear what this is supposed to accomplish. If you are unsure how to do something basic in C++, like this, then a good C++ textbook would be a good resource for this. Unfortunately, Stackoverflow does not really work as a textbook replacement (we just answer ***specific***, detailed questions here), and C++ is just too complex to be learned by reading other programs, try to get a good C++ textbook to learn how to to use `std::ostream`. – Sam Varshavchik Dec 26 '22 at 23:23
  • _the coding I know I learned from Arduino projects and many, many online tutorials_ This is not a good way to learn the language. Some inspiration [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Paul Sanders Dec 26 '22 at 23:58

1 Answers1

1

I figured it out, as it turns out I was 90% there, I had to do a little research on how std::ostream works, but all I needed was some insertion operands that I missed and adjust the ofstream command syntax:

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

uint32_t value = 0;
uint8_t bytes[8][262144] = {0x00};

int main()
{
    //Program data bytes
    cout <<"Writing Binary File2";

    int8_t digits[] = {0X3f, 0X06, 0X5b, 0X4f, 0X66, 0X6d, 0X7d, 0X07, 0X7f, 0X6f, 0X77, 0X7c, 0X39, 0X5e, 0X79, 0X71 }; // 7-segment display output bytes for hexidecimal

    std::ofstream fout1("HexDisplay.bin");

    if (!fout1) {
        std::cerr << "Error opening output files.\n";
        return 1;
    }

    for (value = 0; value <= 65535; value +=1) {
        fout1 <<(value + 0, digits[(value / 1) % 16]);
    }
    cout <<("OnesDone!..."); //


    for (value = 0; value <= 65535; value +=1) {
        fout1 <<(value + 65536, digits[(value / 16) % 16]);
    }
    cout <<("TensDone!...");

    for (value = 0; value <= 65535; value +=1) {
        fout1 <<(value + 131072, digits[(value / 256) % 16]);
    }
    cout <<("HundredsDone!...");

    for (value = 0; value <= 65535; value +=1) {
        fout1 <<(value + 196608, digits[(value / 4096) % 16]);
    }
    cout <<("ThousandsDone!...");

    cout <<("Programming Complete!...");
}

Works Perfect! Thanks for all your humble advice to a novice.

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
Cramano
  • 9
  • 3
  • 2
    Your use of `operator<<` on the `std::ofstream` doesn't do what you think it does, because 1) your use of the [`comma operator`](https://en.cppreference.com/w/cpp/language/operator_other) is getting in the way, and 2) `operator<<` writes *formatted* output, not binary output. Perhaps you meant to use `ofstream::write()` instead? – Remy Lebeau Dec 27 '22 at 06:12