LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Append a file to another

Hi all,
in my application I need to save an incremental copy of test results to a file. Results are stored in temporary text files and must be appended to final text files. Dimension of temporary files to append is more or less 100 kB at a time.

The ideal would be something similar to the system command "type file1.txt >> file2.txt" which exactly appends one file to another, but I don't know how to do it programmatically. I do prefer not to rely on system commands since the equipment is still executing tests and collecting results while transferring data and I don't want to stop it while executing a command that can take some (unpredictable) time to execute.

Every help is appreciated
Roberto


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 1 of 5
(3,641 Views)
Hi Roberto.

I don't believe that there is a single function that will do all you want, but I see a couple of ways to go.

1. If you really need your temporary file:
- On the temporary file:
-- GetFileSize() / malloc() / OpenFile() / ReadFile() / CloseFile() / DeleteFile()
- On the final file:
-- OpenFile() / WriteFile() / CloseFile()
As you say, this will take some time to execute, so it sould be handled in a separate thread.
I use an asynchronous timer to trigger the reading and processing of data from my acquisition in a dedicated thread (not the main thread). From this thread I call PostDeferredCall() or CmtScheduleThreadPoolFunction() to do any disk & UI updates, and communications.

2. If you don't need the temporary file:
Use double buffering. Set up two global text buffers (char ** buf[2]).
- Start storing your results in buf[0]
- After 1 minute (say), or when you have enough data:
-- Switch to storing results in buf[1]
-- Call CmtScheduleThreadPoolFunction(), passing (void *)(buf[0]) as a parameter
-- Do your disk write operation in the scheduled function.
- Repeat, alternating between the two buffers
You will need to include some synchronising logic for safety (e.g. using thread locks).

3. [This is something I have not tried] Use Windows SDK functions WriteFile(), WriteFileEx() etc. These can operate asynchronously, i.e. the function can return before the physical disk write is completed.

Good luck.
Colin.
0 Kudos
Message 2 of 5
(3,606 Views)
This is a console application. you can launch it with the following command "fileappend source.txt target.txt"
or in a CVI application using the system command. It'll run on XP. I have the source so if you want a minor change I can help.

Steve
0 Kudos
Message 3 of 5
(3,607 Views)
For some reason I can't attach the exe.... If you want to use it fell free to contact me.
0 Kudos
Message 4 of 5
(3,595 Views)
Thank you all.

Colin.
I am already using an asynchronous timer for my acquisition / control, so I'm quite sure in using open-read-write-close sequence. I asked for an alternative mainly to simplify the code: one function, one error code and nothing like open-the-first-file-check-the-error / open-the-second-file-check-the-error / read-check-the-error and so on. 😉

Unfortunately the temporary file is necessary to me since the user wants to read test results during test execution so I want to minimize access to data file to reduce the possibility of file conflicts.

Steve.
I'll take a look to your program, even if I think in present application the other solution is safer. Thank you anyway.


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 5 of 5
(3,585 Views)