08-25-2015 12:13 PM
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.
08-25-2015 01:24 PM - edited 08-25-2015 01:25 PM
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.
08-25-2015 03:03 PM - edited 08-25-2015 03:05 PM
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.
08-25-2015 03:09 PM - edited 08-25-2015 03:16 PM
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.
08-25-2015 04:38 PM
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.
08-25-2015 10:11 PM
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
08-26-2015 01:27 AM
It seems that you're learning C, so here are some resources that may help you further on this way:
08-26-2015 01:46 AM
The use of the asterisk is fully described at the end of the help page I pointed you to: see
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.
08-26-2015 09:41 AM
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.
08-26-2015 12:47 PM
Thanks blanky. when i will read till the end of file then how would i reposition the pointer?