<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Write Strings to file in LabWindows/CVI</title>
    <link>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875720#M40468</link>
    <description>&lt;P&gt;Hi Roberto,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your WriteLine (...)&amp;nbsp; works better than the WriteStringToFile (...) which is also a library but function but it shows CR, LF etc that are unrecognized.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
    <pubDate>Mon, 23 Mar 2009 08:12:58 GMT</pubDate>
    <dc:creator>NI Nubie</dc:creator>
    <dc:date>2009-03-23T08:12:58Z</dc:date>
    <item>
      <title>Write Strings to file</title>
      <link>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875610#M40462</link>
      <description>&lt;P&gt;I want to write text to a file but the CR and NL do not work and the data is on one continous line. I tried adding CR, NL, and NULL but tono avail.&lt;/P&gt;&lt;P&gt;I can write the strings to a list box with no problem but when writing to a file the new lines do not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#define FILE_NAME "Data.txt"&lt;/P&gt;&lt;P&gt;static FILE *fname;&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;main&lt;/P&gt;&lt;P&gt;{ &lt;/P&gt;&lt;P&gt;&amp;nbsp;fname = fopen(FILE_NAME, "w");&lt;/P&gt;&lt;P&gt;&amp;nbsp;fclose(fname);&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;void EnterDisplayRxElement (char* readbuf, int len, int display_ind)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; char temp[200] = {0};&lt;BR /&gt;&amp;nbsp; char buf[200] = {0};&lt;BR /&gt;&amp;nbsp; char buf1[200] = {0};&lt;BR /&gt;&amp;nbsp; char buf2[200] = {0};&lt;BR /&gt;&amp;nbsp; int pos;&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* This string for list box. */&lt;BR /&gt;&amp;nbsp; strncpy (temp, readbuf, len);&lt;BR /&gt;&amp;nbsp; sprintf(buf, "\033fg%s", BLUE);&lt;BR /&gt;&amp;nbsp; strncat (buf, readbuf, len);&lt;BR /&gt;&amp;nbsp; /* add line number to the end of the line for testing. Romove later. */&lt;BR /&gt;&amp;nbsp; sprintf(buf1, " %d", displaybuf[display_ind].numActRows);&lt;BR /&gt;&amp;nbsp; strcat (buf, buf1);&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* This string for file. */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; strncpy (buf2, readbuf, len);&lt;BR /&gt;&amp;nbsp; strncat(buf2, CR_STR, 1);&lt;BR /&gt;&amp;nbsp; strncat(buf2, NL_STR, 1);&lt;BR /&gt;&amp;nbsp; strncat(buf2, NULL_STR, 1);&lt;/P&gt;&lt;P&gt;&amp;nbsp; // I tried all of these and none work. &lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; /* Write string to file. */&lt;/P&gt;&lt;P&gt;&amp;nbsp; WriteStringToFile (fname, buf2);&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; /* This string for listbox. */&amp;nbsp; &lt;BR /&gt;&amp;nbsp; pos = displaybuf[display_ind].numActRows;&lt;BR /&gt;&amp;nbsp; strcpy (displaybuf[display_ind].row[pos].item, buf);&lt;BR /&gt;&amp;nbsp; displaybuf[display_ind].numActRows++;&lt;BR /&gt;&amp;nbsp; displaybuf[display_ind].updateDisplay = UPDATE_DISPLAY;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;int UpdateDisplays (int index, int panel)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; int i;&lt;BR /&gt;&amp;nbsp; int first_visible_line = 0;&lt;BR /&gt;&amp;nbsp; int result = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; ClearListCtrl (panel, PANEL_LISTBOX);&lt;/P&gt;&lt;P&gt;&amp;nbsp; for (i = 0; i &amp;lt; displaybuf[index].numActRows; i++)&lt;BR /&gt;&amp;nbsp; {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; InsertListItem (gPanelHandle, PANEL_LISTBOX, i, displaybuf[index].row[i].item, 0);&lt;BR /&gt;&amp;nbsp; }/* end for (i=0; i&amp;lt;displaybuf[index].numActRows; i++) */&lt;BR /&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;return (result);&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2009 02:07:34 GMT</pubDate>
      <guid>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875610#M40462</guid>
      <dc:creator>NI Nubie</dc:creator>
      <dc:date>2009-03-23T02:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: Write Strings to file</title>
      <link>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875631#M40463</link>
      <description>&lt;P&gt;I apologize as I do not understand all of your code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've never actually used sprintf, so I am unaware of any advantages.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;char string[260] = "output.txt"; &lt;/P&gt;&lt;P&gt;Usually I use fprintf(FILE *stream,"%s\n",string); &lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this helps! &lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2009 03:22:24 GMT</pubDate>
      <guid>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875631#M40463</guid>
      <dc:creator>XBrav</dc:creator>
      <dc:date>2009-03-23T03:22:24Z</dc:date>
    </item>
    <item>
      <title>Re: Write Strings to file</title>
      <link>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875665#M40466</link>
      <description>&lt;P&gt;What's in your WriteStringToFile ( ) function?&lt;/P&gt;&lt;P&gt;When I need to write lines of text on a file I normally use OpenFile (...) and WriteLine (...) functions, with the second that automatically writes the necessary CR+LF characters at the end of the line.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2009 05:46:32 GMT</pubDate>
      <guid>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875665#M40466</guid>
      <dc:creator>RobertoBozzolo</dc:creator>
      <dc:date>2009-03-23T05:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: Write Strings to file</title>
      <link>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875702#M40467</link>
      <description>&lt;P&gt;XBrav,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sprintf allows you to create a local string and write characters (%s)&amp;nbsp;to it and then you can strcat() to it before you send it to the file.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2009 07:55:14 GMT</pubDate>
      <guid>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875702#M40467</guid>
      <dc:creator>NI Nubie</dc:creator>
      <dc:date>2009-03-23T07:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: Write Strings to file</title>
      <link>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875720#M40468</link>
      <description>&lt;P&gt;Hi Roberto,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your WriteLine (...)&amp;nbsp; works better than the WriteStringToFile (...) which is also a library but function but it shows CR, LF etc that are unrecognized.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2009 08:12:58 GMT</pubDate>
      <guid>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875720#M40468</guid>
      <dc:creator>NI Nubie</dc:creator>
      <dc:date>2009-03-23T08:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: Write Strings to file</title>
      <link>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875742#M40470</link>
      <description>&lt;P&gt;first problem: have you thoroughly read the documentation for &lt;FONT face="courier new,courier"&gt;strncpy&lt;/FONT&gt;() ? here is the interesting part:&lt;/P&gt;&lt;P&gt;"If no ASCII NUL byte is found within the specified number of bytes, the function 
returns after copying the specified number of bytes and does not append an ASCII 
NUL byte to the buffer."&lt;/P&gt;&lt;P&gt;so, if &lt;FONT face="courier new,courier"&gt;readbuf &lt;/FONT&gt;does not contain a NUL byte within &lt;FONT face="courier new,courier"&gt;len &lt;/FONT&gt;character, there is no NUL byte at the end of &lt;FONT face="courier new,courier"&gt;buf &lt;/FONT&gt;or &lt;FONT face="courier new,courier"&gt;buf2&lt;/FONT&gt;. however, &lt;FONT face="courier new,courier"&gt;strcat() &lt;/FONT&gt;(or &lt;FONT face="courier new,courier"&gt;strncat()&lt;/FONT&gt;) first scans for a NUL byte to know where it should start appending text. so &lt;FONT face="courier new,courier"&gt;strncpy()&lt;/FONT&gt; followed by &lt;FONT face="courier new,courier"&gt;strcat()&lt;/FONT&gt; is a bad idea. &lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;second problem: why are you writing&amp;nbsp; &lt;FONT face="courier new,courier"&gt;&lt;SPAN class="noindex"&gt;strncat(buf2, CR_STR, 1 ) &lt;FONT face="arial,helvetica,sans-serif"&gt;? i assume CR_STR is a constant, thus you already know its length and using this constant is safe. there is no need for &lt;FONT face="courier new,courier"&gt;strncat()&lt;/FONT&gt;, a simple &lt;FONT face="courier new,courier"&gt;strcat()&lt;/FONT&gt; would suffice. &lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;third problem:&amp;nbsp; &lt;SPAN class="noindex"&gt;&lt;FONT face="courier new,courier"&gt;strncat(buf2, NULL_STR, 1)&lt;/FONT&gt;. are you kidding ? what's the point of appending a nul byte at the end of an already nul terminated string ??&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;fourth problem: CR_STR, NL_STR. in C, there is a standard escape character which looks like this: '\n', which is pretty sufficient to add a new line to a string.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;last problem: isn't something missing within you main function ? currently, it only opens the file and immediately closes it. i assume this is a typo and your function actually performs useful things. &lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so here is a modified version of a part of your code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;FONT face="courier new,courier"&gt;strcpy(&amp;nbsp; buf2, "" );&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // first clear the buffer (maybe unnecessary because of the &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // initializer at the declaration of buf2)&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;strncat( buf2,&amp;nbsp; readbuf, len ); // adds the content of readbuf, strncat (contrary to strncpy) &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // always adds a NUL byte at the end of the string&lt;BR /&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;strcat( buf2, "\n" );&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // we know "\n" is a one byte long string, no need for strncat &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="noindex"&gt;fwrite( buf2, sizeof( *buf2 ), strlen( buf2 ), fname );&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;last thing: it is important to know what is written to your file, thus a little breakpoint in the debugger before the call to fwrite() would help to know if your string is correct before writing. from the information you collect in the debugger, you should be able to spot the problem in your code. remember: computers do never make mistakes, but the human who programs the computer do. &lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2009 08:50:24 GMT</pubDate>
      <guid>https://ni.lithium.com/t5/LabWindows-CVI/Write-Strings-to-file/m-p/875742#M40470</guid>
      <dc:creator>dummy_decoy</dc:creator>
      <dc:date>2009-03-23T08:50:24Z</dc:date>
    </item>
  </channel>
</rss>

