LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

double number to string

hi to all of u. i want to convert the array of doubles in to the strings. what function convert double type numbers in to strings. and my second question is  there any function which write the string at a desired offset in textfile. for example i want to write |2.3 in 4th line not at start of line. kindly explain me.

0 Kudos
Message 1 of 25
(5,738 Views)

There are formating functions that can do this the easiest is Fmt().

 

For example

 

void foo (void)

{

 

    char         teststring[100];

    double     array[50];

    int            i;

 

    for (i = 0; i < 50; i++)

         array[i] = i * 88.993;

   for (i = 0; i < 50; i++)

       {

       Fmt (teststring, "   %s", array[i]);   /// Note the 3 spaces before the %s

       // do something with teststring here...

       }

}

 

Note: Fmt() is extremely flexible and simple to use but ANSI C formatting functions tend to be much faster.

0 Kudos
Message 2 of 25
(5,731 Views)

Fmt is indeed a powerful command: with an appropriate format string you can create the output string in a single pass.

Supposing 'c' is an array of n integers, you can format it in a string with semicolon separator using this command:

 

Fmt (buffer, "%*d[j1];%d", n - 1, c, c[n - 1]);

Make reference to the help for the command to understand the use of '*' and 'j' formatting specifier.

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 25
(5,723 Views)

Referring to your second question, you must not imagine a text file as a spreadsheet: if you want to write something in it you must read the file, properly manipulate it's content to insert you text where appropriate and write it down to disk again.

 

Specifically, there is not an easy way to 'write to 4th row': you must read previous 3 rows in a buffer, then append your data to the buffer, read the rest of the file appending to the same buffer and then write all to disk again.

 

Normally this is made by means of temporary output files: the Programmer''s Toolbox provides CreateAndOpenTemporaryFile and Deleteandrename functions to ease this task.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 25
(5,721 Views)

 I kind of assumed by fourth row that he intned to place the number starting as the fourth character (position) in the text string.

I should have clarified.

0 Kudos
Message 5 of 25
(5,718 Views)

thanku so much Roberto. Roberto i was studing the scan functions yesterday. i could not understand the use of asterik symbol(*). why we use this symbol explain me with example.

Also tell me i have a 10 rows in a text file and want to read or pick the rows 4th and 5th directly. Means i do not want to read ist 4 lines. for example

12

1

5

3

8

4

2

3

7

 

now i m using read line function previously it reads from the ist row. i want to pick rows from 5th to end of rows and copy in a text file say khadim.text. then i want to pick ist 4 rows and then append these rows in to khadim.text. for example khadim.txt file will be

 

8

4

2

3

7

12

1

5

3

this is my desired task

 

0 Kudos
Message 6 of 25
(5,709 Views)
0 Kudos
Message 7 of 25
(5,697 Views)

The use of the asterisk is fully described at the end of the help page I pointed you to: see 

Using Asterisks (*) Instead of Constants in Format Specifiers

 

In my example I used it to dynamically state the number of elements in the array so that the function can treat arrays on any length. 

 

As I told you before, a text file cannot be accesses randomly: you need to read it, manipulate full text and rewrite it to have it updated.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 8 of 25
(5,693 Views)

Learning to do this kind of tqask is best handled in a classroom or class text book. But if your task is to rearrange a text file and store it as another (as you have described) then there is no use to use any formatting tools at all.

 

In this case,

 

You would do the following

 

open the input file

open the output file

read the first four lines of input file(to a buffer) Note this is done simply to move the file pointer to the fifth line of the input file ( a for loop would be great here)

read a line  from input file

write a line to output file

repeat above two steps until the end of f ile is reached for the input file

reposition the file pointer to the beginning of the input file

read a line from input

write a line to output

repeat 3 times

close the input file

close the output file

 

This is the basic concept. There are always other ways to do a program but this would likely be considered a pretty classic concept for this type of task. 

 

You will need figure the syntax or you won't learn it well enough to ever repeat it.

 

Note: the numbers in the example appear to be integer rather than double. You would save time and memory to format as integer if that is all that is required for the task.

 

Greg.

 

 

 

0 Kudos
Message 9 of 25
(5,670 Views)

Thanks blanky.  when i will read till the end of file then how would i reposition the pointer?

0 Kudos
Message 10 of 25
(5,662 Views)