LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Changing some lines in .txt file

I want to change a line in the any txt file.
Old line is  "NS(123456789)," . When I change this line with "NS(0000)," ,   old line stay as "NS(000056789),".
How can I reset the old line?
I use Writeline command for this operation
Thanks
 
0 Kudos
Message 1 of 7
(4,165 Views)

Well, it seems that your problem is related to how you update your line rather than to file writing. Basically WriteLine adds a end-of-line character at the end of the string before writing it to the file, so in my opinion you are not preparing correctly the string to write down. How are you updating the line?

You could put a breakpoint in your code immediately before witing to the file and look at the line content to detect if it is correct or not.



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 2 of 7
(4,139 Views)

   index = FindPattern (readingline, 0, -1, "NS1(", 0, 0);
    if (index == 0)
     {
          SetFilePtr (file_handle, -(numreadingline + 1), 1);
     main_str = "NS(789),\0";
     WriteFile (file_handle, main_str, StringLength(main_str));

      break;
     }

0 Kudos
Message 3 of 7
(4,135 Views)
Dear Roberto,
 
I'm sending the related code.
 
 
 
 index = FindPattern (readingline, 0, -1, "NS(", 0, 0);
    if (index == 0)
     {
          SetFilePtr (file_handle, -(numreadingline + 1), 1);
          main_str = "NS(789),\0";
          WriteFile (file_handle, main_str, StringLength(main_str));
          break;
     }
 
 
 
I couldn't find that I prepare the line to writing in different style
 
Thank you
 
 
0 Kudos
Message 4 of 7
(4,134 Views)

Hello,

If alos the '\0' character should be written, your code should be:

WriteFile (file_handle, main_str, StringLength(main_str+1));

According to StringLength help, StringLength returns the number of bytes in the string up to, but not including, the first ASCII NUL.

 

0 Kudos
Message 5 of 7
(4,129 Views)

Sorry, that should have been

WriteFile (file_handle, main_str, StringLength(main_str)+1);

Smiley Wink

0 Kudos
Message 6 of 7
(4,130 Views)
Basically, a txt file is an ascii file written in rows each of them has a proper line terminator (which, btw, is different depending on the OS you are running). The text is written in the file sequentially without  padding, alignment or extra characters: all the characters that are present in the files are written if you open a txt file in notepad or any text editor and interpreted as if they were alphanumeric caracters (only the line terminator is not shown in the editor).
 
For this reason, if you need to change a line in a text file with a new line with different lenght you must rearrange all the content of the file that follows the line edited, that must be shifted backwards if the new line is shorter that previous one or to add extra space if the new line is longer.
 
Since you are using WriteFile instead of WriteLine you are missing to add the line terminator at the end of the edited text. Moreover, you are nor rearranging the following line content to adapt to the new line lenght: that is why you are reading back the edited line that way.
 
Since you are already reading all the file to detect the correct line to edit, you could structure your app as follows:
 
Open existing file (source)
Open new (temporary) file
Iterate until source file end
   Read a line (ReadLine) from the source file in a string
   Detect if the line has to be edited. If so:
      Edit the string with the new content
   Write the string to the temporary file (WriteLine
 
At file end:
   Close all files
   Delete previous source file
   Rename temporary file with the source file name
 
Obviously you'll have to add all error checking that is missing.
 
This structure preserves old file content until the last so that if an error occurs this file is not lost.
 

Message Edited by Roberto Bozzolo on 01-27-2006 10:17 AM



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?
Message 7 of 7
(4,121 Views)