C++/How to use write() function with float values ?
Expert: Prince M. Premnath - 11/13/2007
Question#include<fstream.h>
#include<io.h>
#include<dos.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
float num;
char string[8];
int i1,i2;
fstream f1;
num=12345678;
cout<<"
Enter a strig of 8 chars
";
cin>>string;
i1=creatnew("d:\file1.dat",FA_HIDDEN);
i2=creatnew("d:\file2.dat",FA_HIDDEN);
if(i1==0&&i2==0)
cout<<"
files created
";
else if(i1==1&&i2==0)
cout<<"
files not created
";
else
cout<<"
Nothing has been performed
";
f1.open("d:\file1.dat",ios::out);
for(long int i=0;i<100000;i++)
write(i1,string,strlen(string));
f1.close();
f1.open("d:\file2.dat",ios::out|ios::binary);
for(i=0;i<100000;i++)
write(i2,num,sizeof(num));
f1.close();
getch();
}
even if i typecast the num variable as char then also it is not working and giving error .
i m using turbo c++ and windows os.
can u explain me in detail why it is giving error?
and can u also tell me the idea of or concept that how can i run audio files in c++.
please also tell me what all formats can be played in c++.
does it possible to play any format of audio file in c++?
AnswerHi dear Naveen !
Nothing sounds serious with this program , just take a look at the prototype of the write function
write (int handle , void* buffer , unsigned len);
make sure that the second argument should points the buffer where the function writes the byte from ..
>>write(i2,num,sizeof(num));
but in your program u explicitly sending the variable ( thats too a float variable ) thats what your program displaying the scary error message "Incompatible Type conversion "
a better way to deal with this problem is to convert the float number to string using fcvt() function then write the content onto the file , while retrieving the data's just use atof() function to convert the string back to a float number ..
So far i didn't handled sound/ audio files , so i can't help you out with your second question , i kindly request you to look for other c++ expert those who familiar with
Thanks and Regards !
Prince M.Premnath