LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Write Strings to file

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.

I can write the strings to a list box with no problem but when writing to a file the new lines do not work.

 

#define FILE_NAME "Data.txt"

static FILE *fname;
 
main

{

 fname = fopen(FILE_NAME, "w");

 fclose(fname);

}

 

void EnterDisplayRxElement (char* readbuf, int len, int display_ind)
{
  char temp[200] = {0};
  char buf[200] = {0};
  char buf1[200] = {0};
  char buf2[200] = {0};
  int pos;

  /* This string for list box. */
  strncpy (temp, readbuf, len);
  sprintf(buf, "\033fg%s", BLUE);
  strncat (buf, readbuf, len);
  /* add line number to the end of the line for testing. Romove later. */
  sprintf(buf1, " %d", displaybuf[display_ind].numActRows);
  strcat (buf, buf1);

  /* This string for file. */ 
  strncpy (buf2, readbuf, len);
  strncat(buf2, CR_STR, 1);
  strncat(buf2, NL_STR, 1);
  strncat(buf2, NULL_STR, 1);

  // I tried all of these and none work.

 

  /* Write string to file. */

  WriteStringToFile (fname, buf2);
 
  /* This string for listbox. */ 
  pos = displaybuf[display_ind].numActRows;
  strcpy (displaybuf[display_ind].row[pos].item, buf);
  displaybuf[display_ind].numActRows++;
  displaybuf[display_ind].updateDisplay = UPDATE_DISPLAY;
}

 

 

int UpdateDisplays (int index, int panel)
{
  int i;
  int first_visible_line = 0;
  int result = 0;

  ClearListCtrl (panel, PANEL_LISTBOX);

  for (i = 0; i < displaybuf[index].numActRows; i++)
  {
     InsertListItem (gPanelHandle, PANEL_LISTBOX, i, displaybuf[index].row[i].item, 0);
  }/* end for (i=0; i<displaybuf[index].numActRows; i++) */
.

.

.

.


return (result);
}

0 Kudos
Message 1 of 6
(4,204 Views)

I apologize as I do not understand all of your code.

 

I've never actually used sprintf, so I am unaware of any advantages.

 

 

char string[260] = "output.txt";

Usually I use fprintf(FILE *stream,"%s\n",string);

 

Hope this helps!

0 Kudos
Message 2 of 6
(4,201 Views)

What's in your WriteStringToFile ( ) function?

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.



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 3 of 6
(4,195 Views)

XBrav,

 

sprintf allows you to create a local string and write characters (%s) to it and then you can strcat() to it before you send it to the file.

0 Kudos
Message 4 of 6
(4,184 Views)

Hi Roberto,

 

Your WriteLine (...)  works better than the WriteStringToFile (...) which is also a library but function but it shows CR, LF etc that are unrecognized.

 

Thanks.

0 Kudos
Message 5 of 6
(4,177 Views)

first problem: have you thoroughly read the documentation for strncpy() ? here is the interesting part:

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

so, if readbuf does not contain a NUL byte within len character, there is no NUL byte at the end of buf or buf2. however, strcat() (or strncat()) first scans for a NUL byte to know where it should start appending text. so strncpy() followed by strcat() is a bad idea.

 

second problem: why are you writing  strncat(buf2, CR_STR, 1 ) ? i assume CR_STR is a constant, thus you already know its length and using this constant is safe. there is no need for strncat(), a simple strcat() would suffice.

 

third problem:  strncat(buf2, NULL_STR, 1). are you kidding ? what's the point of appending a nul byte at the end of an already nul terminated string ??

 

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.

 

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.

 

so here is a modified version of a part of your code:

 strcpy(  buf2, "" );            // first clear the buffer (maybe unnecessary because of the

                                // initializer at the declaration of buf2)

strncat( buf2,  readbuf, len ); // adds the content of readbuf, strncat (contrary to strncpy)

                                // always adds a NUL byte at the end of the string

strcat( buf2, "\n" );           // we know "\n" is a one byte long string, no need for strncat

fwrite( buf2, sizeof( *buf2 ), strlen( buf2 ), fname );

 

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.

0 Kudos
Message 6 of 6
(4,171 Views)