01-27-2012 10:03 AM
Hello, are there any function that allow me update the information in a determinated row of a .txt file? i hope anybody can help me and thanks in advance.
Solved! Go to Solution.
01-27-2012 10:08 AM
Yes, indirectly...
You can open the file, read it, replace the contents, and save the new data. For this you can either use the standard C functions such as fopen (); fread (), fwrite, and fclose(), or the similar functions (ScanFile / FmtFile...) from the Formatting and I/O Library...
01-30-2012 09:29 AM
Hi,
You can use the function "SetFilePtr" to set the position in the file you want to update. Then, you just have to use the "WriteFile" function to update the info in the text file. There is a simple example included in the Function Help of SetFilePtr in CVI:
/* Open or create the file c:\TEST.DAT, move 10 bytes into the file, and write a string to the file. */
/* Note: In C, use \\ in pathname instead of \. */
int handle, result;
long position;
handle = OpenFile("c:\\TEST.DAT", 0, 2, 1);
if (handle == –1){
FmtOut("error opening file");
exit(1);
}
position = SetFilePtr(handle, 10L, 0);
if (position == 10){
result = WriteFile(handle, "Hello, World!", 13);
if (result == -1)
FmtOut("error writing to file");
}
else
FmtOut("error positioning file pointer");
CloseFile(handle);
Hope this helps!!
Regards!
Anuar Rojas
01-30-2012 09:46 AM
Thanks a lot for the help, it was really useful
01-30-2012 10:31 AM
As a marginal note, I would be careful using this approach because it does not update a specific row. If you start with a text file such as
first row
second row
third row
fourth row
you will end up with (adapting a position of 11 instead of 10 as in the above example):
first row
Hello, World!rd row
fourth row
01-30-2012 11:22 AM
OK Thanks a lot, you provide a very good info.