From: Jeenu on
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
From: Janis on
On 3 Apr., 13:36, Jeenu <jee...(a)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 = 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.

$ MyFloatData=3.513423
$ printf "%g\n" "$MyFloatData"
3.51342
$ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
12.344141

Is that what you want?

>
> Thanks
> Jeenu

From: Jeenu on
On Apr 3, 4:51 pm, Janis <janis_papanag...(a)hotmail.com> wrote:
> On 3 Apr., 13:36, Jeenu <jee...(a)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 = 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.
>
> $ MyFloatData=3.513423
> $ printf "%g\n" "$MyFloatData"
> 3.51342
> $ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
> 12.344141
>
> Is that what you want?
>
>
>
> > Thanks
> > Jeenu

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.
From: Ed Morton on


On 4/3/2008 6:56 AM, Jeenu wrote:
> On Apr 3, 4:51 pm, Janis <janis_papanag...(a)hotmail.com> wrote:
>
>>On 3 Apr., 13:36, Jeenu <jee...(a)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 = 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.
>>
>>$ MyFloatData=3.513423
>>$ printf "%g\n" "$MyFloatData"
>>3.51342
>>$ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
>>12.344141
>>
>>Is that what you want?
>>
>>
>>
>>
>>>Thanks
>>>Jeenu
>>
>
> 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.

From: Jeenu on
On Apr 3, 5:05 pm, Ed Morton <mor...(a)lsupcaemnt.com> wrote:
> On 4/3/2008 6:56 AM, Jeenu wrote:
>
>
>
> > On Apr 3, 4:51 pm, Janis <janis_papanag...(a)hotmail.com> wrote:
>
> >>On 3 Apr., 13:36, Jeenu <jee...(a)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 = 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.
>
> >>$ MyFloatData=3.513423
> >>$ printf "%g\n" "$MyFloatData"
> >>3.51342
> >>$ printf "%s\n" "$MyFloatData * $MyFloatData" | bc
> >>12.344141
>
> >>Is that what you want?
>
> >>>Thanks
> >>>Jeenu
>
> > 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.

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"