Skip to content
  • 0 Votes
    1 Posts
    904 Views
    No one has replied
  • How to use fprintf?

    Unsolved General and Desktop
    9
    0 Votes
    9 Posts
    7k Views
    kshegunovK

    @JohanSolo

    Your first trial should have difficulties to compile, or should at least crash when running, the char and int sizes are different: your read_buff is char*, i.e. each element is 1 byte, whilst the "%d" expects an object of at least 4 bytes.

    While I agree in principle, most compilers don't care. This is C with variadic function(s) which basically means you can forget type-safety. What's happening is that fprintf if just reading memory it doesn't own (which most compilers will allow freely, i.e. it won't crash).

    @Anas_Deshmukh

    int empId1; // some integer FILE * pun; // file pointer char read_buff[] = { 0xAA, 0x2D, 0xFF }; // char array holding value as 0xAA, 0x2D, 0xFF . . . int read_buff_size = 3; //< Size of the above buffer (in elements)

    You can do it like this:

    QFile file; if (!file.open(pun, QFile::WriteOnly)) { // Can't open, error handle accordingly. } QTextStream out(&file); out << empId1; for (qint32 i = 0; i < read_buff_size; i++) out << "~0x" << QString::number(read_buff[i], 16); out << endl; file.close(); // Do not forget to close the FILE *

    Kind regards.

  • 0 Votes
    9 Posts
    4k Views
    SGaistS

    Hi @asthana-ujjwal and welcome to devnet,

    Can you show your .pro file ?