How to use fprintf?
-
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]); }
-
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?
-
@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 } } }
-
hi
do you call fclose(pun) ? -
@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? -
hi there,
try1 ::
fprintf(pun,"%d,",read_buf[i]) // this line wont work and i need alternative for this but this wont work with Qttry2 ::
// after trial and err below work with Qt.
// no use of adding extra %d and empId1. but it wont work with trial 1fprintf(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()?
-
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....
-
Your first trial should have difficulties to compile, or should at least crash when running, the
char
andint
sizes are different: yourread_buff
ischar*
, 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...
-
Your first trial should have difficulties to compile, or should at least crash when running, the
char
andint
sizes are different: yourread_buff
ischar*
, 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...
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).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.