Code Comments
Programming Forum and web based access to our favorite programming groups.Hi Folks, I'm wondering whether I could achieve something like this from a shell: Suppose I do this from a C program: MyFloatData = 3.513423; fwrite(MyFloatData, sizeof(float), 1, pFile); is there any way to retrieve the string "3.513243" at the shell? I would like to pass the string later on to bc or expr for processing. Thanks Jeenu
Post Follow-up to this messageOn 3 Apr., 13:36, Jeenu <jee...@gmail.com> wrote: > Hi Folks, > > I'm wondering whether I could achieve something like this from a > shell: > Suppose I do this from a C program: > > MyFloatData =3D 3.513423; > fwrite(MyFloatData, sizeof(float), 1, pFile); > > is there any =A0way to retrieve the string "3.513243" at the shell? I > would like to pass the string later on to bc or expr for processing. $ MyFloatData=3D3.513423 $ printf "%g\n" "$MyFloatData" 3.51342 $ printf "%s\n" "$MyFloatData * $MyFloatData" | bc 12.344141 Is that what you want? > > Thanks > Jeenu
Post Follow-up to this messageOn Apr 3, 4:51 pm, Janis <janis_papanag...@hotmail.com> wrote: > On 3 Apr., 13:36, Jeenu <jee...@gmail.com> wrote: > > > > > > $ MyFloatData=3.513423 > $ printf "%g\n" "$MyFloatData" > 3.51342 > $ printf "%s\n" "$MyFloatData * $MyFloatData" | bc > 12.344141 > > Is that what you want? > > > I'm afraid not. I wanted to read the binary data from file. Following what you wrote: MyFloatData=$(some_command_or_commands myfile) # MyFloatData should now contain 3.513423 where "myfile" is to where the fwrite output went in OP.
Post Follow-up to this message
On 4/3/2008 6:56 AM, Jeenu wrote:
> On Apr 3, 4:51 pm, Janis <janis_papanag...@hotmail.com> wrote:
>
>
> I'm afraid not. I wanted to read the binary data from file. Following
> what you wrote:
>
> MyFloatData=$(some_command_or_commands myfile) # MyFloatData should
> now contain 3.513423
>
> where "myfile" is to where the fwrite output went in OP.
$ cat myfile
3.513423
$ awk '{print $1 "^2 = " $1 * $1}' myfile
3.513423^2 = 12.3441
Like that?
Ed.
Post Follow-up to this messageOn Apr 3, 5:05 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 4/3/2008 6:56 AM, Jeenu wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> $ cat myfile
> 3.513423
> $ awk '{print $1 "^2 = " $1 * $1}' myfile
> 3.513423^2 = 12.3441
>
> Like that?
>
> Ed.
Still no :)
When I did fwrite in my C program, the file does no more contain the
string "3.513423", instead it's 4-byte (being of type float) *binary
data* - unlike fprintf. So `cat myfile` would give you only some non-
printable characters. My intention is to do something in shell, what
this would do in a C program:
fread(&MyFloatData, sizeof(float), 1, pFile);
sprintf(MyString, "%f", MyFloatData); /* MyString is of type char* */
printf("%s\n", MyString);
Here, program will print "3.513423"
Post Follow-up to this messageJeenu wrote: > Still no :) > > When I did fwrite in my C program, the file does no more contain the > string "3.513423", instead it's 4-byte (being of type float) *binary > data* - unlike fprintf. In other words, you want to read the IEEE (or similar) representation of a float and convert it to a string, IIUC. If everything else fails, you can always write a small program that fread()s the data from the file and does a printf on stdout. -- All the commands are tested with bash and GNU tools, so they may use nonstandard features. I try to mention when something is nonstandard (if I'm aware of that), but I may miss something. Corrections are welcome.
Post Follow-up to this messageOn Apr 3, 5:42 pm, pk <p...@pk.invalid> wrote: > Jeenu wrote: > > > In other words, you want to read the IEEE (or similar) representation of a > float and convert it to a string, IIUC. > If everything else fails, you can always write a small program that fread( )s > the data from the file and does a printf on stdout. > > -- > All the commands are tested with bash and GNU tools, so they may use > nonstandard features. I try to mention when something is nonstandard (if > I'm aware of that), but I may miss something. Corrections are welcome. Yup, exactly. I could always use od or xxd to dump other binary data; since float has a special bit encoding it's not possible ordinarily. And yes, if nothing helps, I'll have to stick to my small program.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.