LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

update info in specific row in txt file

Solved!
Go to solution

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.

0 Kudos
Message 1 of 6
(3,499 Views)

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...

0 Kudos
Message 2 of 6
(3,498 Views)
Solution
Accepted by topic author mkmoon

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:

 

Example Code

/* 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

Anuar R.
National Instruments México y Latinoamérica
Ingeniería de Aplicaciones
www.ni.com/soporte
Message 3 of 6
(3,487 Views)

Thanks a lot for the help, it was really useful

0 Kudos
Message 4 of 6
(3,480 Views)

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

Message 5 of 6
(3,474 Views)

OK Thanks a lot, you provide a very good info.

0 Kudos
Message 6 of 6
(3,469 Views)