Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use fprintf?

How to use fprintf?

Scheduled Pinned Locked Moved Unsolved General and Desktop
c language
9 Posts 4 Posters 7.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anas_Deshmukh
    wrote on 25 May 2016, 06:33 last edited by A Former User
    #1

    hello everyone,

    i am calling c function from qt in which i need to use fprintf syntax but unable to do so.
    i am very thankful if you provide me correct way or other alternative.

    my sample code :

    int empId1 // some integer
    pun // file pointer
    read_buff[i] // char array holding value as 0xAA, 0x2D, 0xFF . . .
    
    

    my aim is to write data into .txt file from read_buff[], but unable to use fprintf my code generate blank file.
    after writing txt file should content like 0xAA, 0x2D, 0xFF . . .

    for(i=10;i<50;i++)
    {
    fprintf(pun,"%d~0x%02X,",empId1,read_buf[i]);
    }
    
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      JohanSolo
      wrote on 25 May 2016, 06:38 last edited by JohanSolo
      #2

      Your problem is not related to Qt...
      Did you check the file was correctly opened? Did you close the file correctly?

      How did you declare your read_buff array? (Basically what its type?). COuld you try to implement a complete and simple program (whitout Qt) which reproduces the same behaviour?

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Anas_Deshmukh
        wrote on 25 May 2016, 09:38 last edited by
        #3

        @JohanSolo thanks for your prompt reply.
        below part of code works fine with c application, i need to do same thing using Qt .

        is there any alternative doin same thing using qt,

        int empId1=1001 
        FILE *pun=NULL;
        char read_buf[50];
        char filename[100]={0};
        sprintf(filename,"/usr/test/database/fp/%d.txt",empId1);
        pun=fopen(filename,"a+");
        		if(!pun)
        		{
        			printf("Couldnt open a file.\n");
                    return -1;
        		}
        		else
        		{
                         for(i=10;i<50;i++){
                         fprintf(pun,"%d~0x%02X,",empId1,read_buf[i]); // this line works in qt only if i add empId1 or any other string
        		  //fprintf(pun,"%d,",read_buf[i]); // this line work in normal c code
        			}
        		}
            }
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 25 May 2016, 09:47 last edited by
          #4

          hi
          do you call fclose(pun) ?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JohanSolo
            wrote on 25 May 2016, 10:13 last edited by JohanSolo
            #5

            @Anas_Deshmukh said:

            fprintf(pun,"%d~0x%02X,",empId1,read_buf[i]); // this line works in qt only if i add empId1 or any other string

            What do mean by "only if I add empId1" ?

            Edit:
            Why do you add the extra "%d~" if you only want something like 0xAA,0x0B,... in your file?

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Anas_Deshmukh
              wrote on 25 May 2016, 11:10 last edited by
              #6

              hi there,

              try1 ::
              fprintf(pun,"%d,",read_buf[i]) // this line wont work and i need alternative for this but this wont work with Qt

              try2 ::
              // after trial and err below work with Qt.
              // no use of adding extra %d and empId1. but it wont work with trial 1

              fprintf(pun,"%d~0x%02X,",empId1,read_buf[i]);

              to avoide confusion just let me know any alternative in Qt which work exactly same as fprintf()?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Anas_Deshmukh
                wrote on 25 May 2016, 11:15 last edited by
                #7
                fprintf(pun,"%d,",read_buf[i])
                

                like above fprintf() is any same API or function in qt so that i write my data into the text file...when i used above fprintf() in qt the text file is created but data was not written into the file....it was create only blank file....

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  JohanSolo
                  wrote on 25 May 2016, 11:16 last edited by
                  #8

                  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.

                  Did you check that the compiler is not giving any warnings? Many problems can be solved by looking at the compiler output...

                  `They did not know it was impossible, so they did it.'
                  -- Mark Twain

                  K 1 Reply Last reply 25 May 2016, 17:45
                  0
                  • J JohanSolo
                    25 May 2016, 11:16

                    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.

                    Did you check that the compiler is not giving any warnings? Many problems can be solved by looking at the compiler output...

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 25 May 2016, 17:45 last edited by
                    #9

                    @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.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0

                    8/9

                    25 May 2016, 11:16

                    • Login

                    • Login or register to search.
                    8 out of 9
                    • First post
                      8/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved