Writing Vector object into a .txt file
-
Hey Thank you Jochen!. Im just wondering how can i make use of a buffer where intitially I can dumb my data..and once the execution is completed writing into a file..do you have a suggestion?.
You can do it similar using
sprintf()
to print to a buffer and then write the buffer to file:char buffer[128];
char *p = buffer;
int n = sprintf(p, "%lf %lf %lf\r\n", newForce.x, newForce.y, newForce.z);
s += n;
sprintf(p, "%lf %lf %lf\r\n", newForce1.x, newForce1.y, newForce1.z);
FILE *f = fopen(fileName, "wb");
fwrite(buffer, 1, strlen(buffer), f);
fclose(f);But as you can see this is much more code and it is insecure without checking for buffer overflows (which adds more code). If you don't need the data to be in text format (e.g. only loaded by your application), it might be better to write the data to a binary file. That solves also the problem of inaccuracies when converting floating point values to text and back again later. Example:
FILE *f = fopen(fileName, "wb");
fwrite(&newForce.x, sizeof(newForce.x), 1, f);
// Write other items here
fclose(f);Reading is then done in a similar way:
FILE *f = fopen(fileName, "rb");
fread(&newForce.x, sizeof(newForce.x), 1, f);
// Read other items here
fclose(f); -
You can do it similar using
sprintf()
to print to a buffer and then write the buffer to file:char buffer[128];
char *p = buffer;
int n = sprintf(p, "%lf %lf %lf\r\n", newForce.x, newForce.y, newForce.z);
s += n;
sprintf(p, "%lf %lf %lf\r\n", newForce1.x, newForce1.y, newForce1.z);
FILE *f = fopen(fileName, "wb");
fwrite(buffer, 1, strlen(buffer), f);
fclose(f);But as you can see this is much more code and it is insecure without checking for buffer overflows (which adds more code). If you don't need the data to be in text format (e.g. only loaded by your application), it might be better to write the data to a binary file. That solves also the problem of inaccuracies when converting floating point values to text and back again later. Example:
FILE *f = fopen(fileName, "wb");
fwrite(&newForce.x, sizeof(newForce.x), 1, f);
// Write other items here
fclose(f);Reading is then done in a similar way:
FILE *f = fopen(fileName, "rb");
fread(&newForce.x, sizeof(newForce.x), 1, f);
// Read other items here
fclose(f);Many thanks Jochen!!:thumbsup:. I'm planning to use just one file containing information about two forces, something like this: timestamp newforce(X Y Z) newForce1(X Y Z)) where the values of x,y,z will be in column.
fprintf(pFile, "%lf %lf %lf\r\n %lf %lf %lf\r\n", newForce.x, newForce.y, newForce.z, newForce1.x, newForce1.y, newForce1.z);
do you think it's a right assignment?, I mean how can I write data effectively into the file with proper indentation?
-
Many thanks Jochen!!:thumbsup:. I'm planning to use just one file containing information about two forces, something like this: timestamp newforce(X Y Z) newForce1(X Y Z)) where the values of x,y,z will be in column.
fprintf(pFile, "%lf %lf %lf\r\n %lf %lf %lf\r\n", newForce.x, newForce.y, newForce.z, newForce1.x, newForce1.y, newForce1.z);
do you think it's a right assignment?, I mean how can I write data effectively into the file with proper indentation?
You can use the printf format specifiers (see http://www.cplusplus.com/reference/cstdio/printf/[^]). Using the
width
specifier, all numbers will have the same width padded with blanks. For proper alignment of the decimal point you should also use theprecision
specifier and theflag
specifier (use a space or minus). The final values to be used depend on the range of your values and the required precision. Assuming a range up to (but excluding) 100 and a precision of 2 fractional digits: Precision = 2 Width = 1 (sign) + 2 (digits) + 1 (decimal point) + 2 (digits) = 6 Then use the format "% 6.2f" for each value:fprintf(pFile, "% 6.2f % 6.2f % 6.2f\r\n% 6.2f % 6.2f % 6.2f\r\n",
newForce.x, newForce.y, newForce.z, newForce1.x, newForce1.y, newForce1.z);For the time stamp you can use the strftime()[^] function.
-
You can use the printf format specifiers (see http://www.cplusplus.com/reference/cstdio/printf/[^]). Using the
width
specifier, all numbers will have the same width padded with blanks. For proper alignment of the decimal point you should also use theprecision
specifier and theflag
specifier (use a space or minus). The final values to be used depend on the range of your values and the required precision. Assuming a range up to (but excluding) 100 and a precision of 2 fractional digits: Precision = 2 Width = 1 (sign) + 2 (digits) + 1 (decimal point) + 2 (digits) = 6 Then use the format "% 6.2f" for each value:fprintf(pFile, "% 6.2f % 6.2f % 6.2f\r\n% 6.2f % 6.2f % 6.2f\r\n",
newForce.x, newForce.y, newForce.z, newForce1.x, newForce1.y, newForce1.z);For the time stamp you can use the strftime()[^] function.
I tried as u suggested , but now the data I'm getting is kind of overflowing, I mean it's always generating a data txt file of 1kb!!
-
I tried as u suggested , but now the data I'm getting is kind of overflowing, I mean it's always generating a data txt file of 1kb!!
You may give an example of your data. Inspecting the text file may also give a hint what is going wrong. [EDIT] I have replaced the usage of "%lf" by "%f" in my post. But this should make no difference with MS compilers.
-
You may give an example of your data. Inspecting the text file may also give a hint what is going wrong. [EDIT] I have replaced the usage of "%lf" by "%f" in my post. But this should make no difference with MS compilers.
Hi Jochen, I'm getting data like this 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 It's 2 raws of numbers..3,5 lines are continuation of 1st line..ans so 4,6 .. It's basically limited to 1kb irrespective of the duration of experiment
-
Hi Jochen, I'm getting data like this 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 18832315686612578000000000000000000000000000000000000000000000000000.00 It's 2 raws of numbers..3,5 lines are continuation of 1st line..ans so 4,6 .. It's basically limited to 1kb irrespective of the duration of experiment
That are quite large numbers. Are they the correct values? If your numbers are in that range, you should use the "%E" format.
-
You may give an example of your data. Inspecting the text file may also give a hint what is going wrong. [EDIT] I have replaced the usage of "%lf" by "%f" in my post. But this should make no difference with MS compilers.
Hi Jochen, Now it's working perfectly!. I did a a mistake in closing the file. Thank you so much for your prompt and precise feedback!.
-
Hi Jochen, Now it's working perfectly!. I did a a mistake in closing the file. Thank you so much for your prompt and precise feedback!.
Fine that all is working now and thank you for the feedback.
-
Fine that all is working now and thank you for the feedback.
Hi Jochen, The data i'm getting is of very large sample. I use a inbuilt thread class calls at very high frequency. Basically I want to reduce the sampling rate so that I'll get significant data with less samples..Do you have any idea how to create periodic thread with a fixed frequency?
-
Hi Jochen, The data i'm getting is of very large sample. I use a inbuilt thread class calls at very high frequency. Basically I want to reduce the sampling rate so that I'll get significant data with less samples..Do you have any idea how to create periodic thread with a fixed frequency?
It is difficult to answer this without knowing how samples are triggered, new data are signaled, and what is done by your thread class. When sampling is triggered by your app, you can control the frequency. If it is free running you may skip samples (e.g. processing only every second one). When using a worker thread you usually call a wait function (e.g.
WaitForSingleObject
) that returns upon events or a time out. The time out value can be used for periodic execution. -
It is difficult to answer this without knowing how samples are triggered, new data are signaled, and what is done by your thread class. When sampling is triggered by your app, you can control the frequency. If it is free running you may skip samples (e.g. processing only every second one). When using a worker thread you usually call a wait function (e.g.
WaitForSingleObject
) that returns upon events or a time out. The time out value can be used for periodic execution.Basically two functions one is for updating graphics and other for haptics rendering. Haptic rendering sampling rate is quite large compared to graphics. In the library I'm using there is a thread class basically gives priority to haptic and graphics rendering. But I'm not able to see any section in the code where they access system timing and setting sampling rate etc. I'm thinking about assigning some condition inside data will be written into the file at a rate of 100 samples per second. There is an inbuilt precision clock with the library, so if I've I can get cpu time with precision clock , how can formulate a condition to write data at a rate 100Hz??
-
Basically two functions one is for updating graphics and other for haptics rendering. Haptic rendering sampling rate is quite large compared to graphics. In the library I'm using there is a thread class basically gives priority to haptic and graphics rendering. But I'm not able to see any section in the code where they access system timing and setting sampling rate etc. I'm thinking about assigning some condition inside data will be written into the file at a rate of 100 samples per second. There is an inbuilt precision clock with the library, so if I've I can get cpu time with precision clock , how can formulate a condition to write data at a rate 100Hz??
As far as I understood you have a haptic device which is some kind of hardware which generates data at a frequency defined by the hardware. If the library does not provide functions to set the sample rate, it is probably fixed. The thread is then activated when new data are available. But again, without knowing the hardware and the library (which acesses the hardware specific driver), it is rather impossible to answer. Do you want to write the data to a file with 100 Hz. This is probably too fast (especially when the file must be opened for each write). It again depends on your requirements (who is reading the file). If the data should be read by another process, you may use some kind of IPC (Inter Process Communication). Regarding timers: Windows has no high resolution timers. While it is possible to measure times with high resolution (QueryPerformanceCounter), the system timers have a resolution of about 10 ms by default and can be tweaked down to 1 ms.
-
As far as I understood you have a haptic device which is some kind of hardware which generates data at a frequency defined by the hardware. If the library does not provide functions to set the sample rate, it is probably fixed. The thread is then activated when new data are available. But again, without knowing the hardware and the library (which acesses the hardware specific driver), it is rather impossible to answer. Do you want to write the data to a file with 100 Hz. This is probably too fast (especially when the file must be opened for each write). It again depends on your requirements (who is reading the file). If the data should be read by another process, you may use some kind of IPC (Inter Process Communication). Regarding timers: Windows has no high resolution timers. While it is possible to measure times with high resolution (QueryPerformanceCounter), the system timers have a resolution of about 10 ms by default and can be tweaked down to 1 ms.
Jochen Arndt wrote:
the system timers have a resolution of about 10 ms by default and can be tweaked down to 1 ms.
What about the waitable timers? SetWaitableTimer[^]
Quote:
pDueTime [in] The time after which the state of the timer is to be set to signaled, in 100 nanosecond intervals.
The difficult we do right away... ...the impossible takes slightly longer.
-
Jochen Arndt wrote:
the system timers have a resolution of about 10 ms by default and can be tweaked down to 1 ms.
What about the waitable timers? SetWaitableTimer[^]
Quote:
pDueTime [in] The time after which the state of the timer is to be set to signaled, in 100 nanosecond intervals.
The difficult we do right away... ...the impossible takes slightly longer.
Windows uses internally 100 ns based times (
FILETIME
structure). But this does not mean that the internal timer tick is running with a resolution of 100 ns. There are undocumented functions to query the range of resolution and set the resolution (NtQueryTimerResolution
,NtSetTimerResolution
). On a typical system, the range is 0.5 to 15.6 ms. -
Windows uses internally 100 ns based times (
FILETIME
structure). But this does not mean that the internal timer tick is running with a resolution of 100 ns. There are undocumented functions to query the range of resolution and set the resolution (NtQueryTimerResolution
,NtSetTimerResolution
). On a typical system, the range is 0.5 to 15.6 ms.How can I configure a periodic thread of particular frequency?
-
How can I configure a periodic thread of particular frequency?
Just call
Sleep()
with a time out value of 1000 / frequency[Hz]. A better implememntation would useWaitForSingleObject()
with the same time out value and a handle to a terminate thread event so that you can terminate the thread:UINT worker_thread_func(LPVOID pParam)
{
// pParam is usually a pointer to a C++ class to which this thread belongs
// that is passed when creating the thread.
// Cast the pointer to get access to member vars.
MyClass *pThis = (MyClass *)pParam;
HANDLE hKillEvent = pThis->m_hKillEvent;
while (1)
{
if (WAIT_OBJECT_0 == WaitForSingleObject(hKillEvent, TIME_OUT_VAL))
break;
// Perform periodic task here
}
return 0;
} -
Just call
Sleep()
with a time out value of 1000 / frequency[Hz]. A better implememntation would useWaitForSingleObject()
with the same time out value and a handle to a terminate thread event so that you can terminate the thread:UINT worker_thread_func(LPVOID pParam)
{
// pParam is usually a pointer to a C++ class to which this thread belongs
// that is passed when creating the thread.
// Cast the pointer to get access to member vars.
MyClass *pThis = (MyClass *)pParam;
HANDLE hKillEvent = pThis->m_hKillEvent;
while (1)
{
if (WAIT_OBJECT_0 == WaitForSingleObject(hKillEvent, TIME_OUT_VAL))
break;
// Perform periodic task here
}
return 0;
}Hi Jochen, I still didn't get idea of using WaitForSingleObject() as a timer. Basically WaitForSingleObject is a predefined function under windows.h? right?. Can you bit explain how exactly this can be embedded with a project?
-
Hi Jochen, I still didn't get idea of using WaitForSingleObject() as a timer. Basically WaitForSingleObject is a predefined function under windows.h? right?. Can you bit explain how exactly this can be embedded with a project?
It is a Windows API function that is used with threads. It will suspend the thread until the event specified by the passed handle occurs or the time out time has elapsed. Using threads is an advanced topic. So you should read about it first. A starting point may be Using Worker Threads[^]. Your requirement was to execute specific tasks in fixed intervals. This can be done by Windows timers from within your main (GUI) thread. But this will block the main thread for the task execution time. With short intervals, this will lead to delayed user input (mouse and keyboard actions are not performed immediately). To avoid this, a worker thread can be used.
-
It is a Windows API function that is used with threads. It will suspend the thread until the event specified by the passed handle occurs or the time out time has elapsed. Using threads is an advanced topic. So you should read about it first. A starting point may be Using Worker Threads[^]. Your requirement was to execute specific tasks in fixed intervals. This can be done by Windows timers from within your main (GUI) thread. But this will block the main thread for the task execution time. With short intervals, this will lead to delayed user input (mouse and keyboard actions are not performed immediately). To avoid this, a worker thread can be used.
There is class precision clock included in the library. So I can use this for getting system time. But my doubt is if I've the 'time', with a conditional statement I should be able down sample data writing to a sampling rate of 100hZ??!. If I've time stamp at rate of 8KHz then how to formulate a conditional statement to write data at 100Hz??