LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting the data from a file

Hi.

I want to write into a file an array of strings and I used ArrayToFile function.
The problem is that I need the information in the file to be in string format not in decimal. In the file appairs
the aschi corespondent of the string.

char ArrayOfStrings[10000][50];

status = ArrayToFile (fileName, ArrayOfStrings, VAL_CHAR,
10000, 10000*50, VAL_GROUPS_TOGETHER,
VAL_GROUPS_AS_ROWS, VAL_CONST_WIDTH, 3, VAL_ASCII, VAL_APPEND);

Can you advise me in this problem?

Thanks.
0 Kudos
Message 1 of 8
(4,437 Views)
Hello,

I do not understand what the difference is between "string" and "decimal". Maybe you can elaborate for other readers also.
However I sometimes have to flush many ASCII formatted values to a file. Opening and closing the file for each value would increase overhead. So I use a kind of diskbuffer. When I use this diskbuffer with sprintf function, I have full control of the ASCII format for output.

// first select (global variable) filename and create file for writing text
fptr = fopen(ExportFileName, "w+t");
if (fptr != NULL)
{
sprintf(LongReportString, "ExportTool in MyProgramme.exe\n");
fputs(LongReportString, fptr);
fflush(fptr);
fclose(fptr);
}
// subsequent send data to disk buffer (see source below)
for (n = 0, n<10000, n++)
{
sprintf(LongReportString, "index: %6d , value: %8.3lf\n", n, mydoublevalue)
WriteToDiskBuffer(0, LongReportString);
}
// finish with flushing after last data set
WriteToDiskBuffer(1, NULL);

Succes, Jos

An example of my source for the diskbuffer:
#define MaxLengthReportString 80
#define MaxReportBufferStrings 10000

// option 1 at global level if used often:
// this option 1 is used in subsequent functions
static char ExportFileName[MAX_PATHNAME_LEN];
static char ReportDiskBuffer[((MaxReportBufferStrings+1)*(MaxLengthReportString+3))] = {0};
// option 2 at function level if used once:
long bufsize;
char * ReportDiskBuffer;
static char ExportFileName[MAX_PATHNAME_LEN];
bufsize = (MaxReportBufferStrings+1)*(MaxLengthReportString+3);
ReportDiskBuffer = (char *)calloc(bufsize, sizeof(char))
... ... ...
free (ReportDiskBuffer)

/***************************************************************************/
/* Function : Write string to - already existing - file */
/* In : FileName, pointer to string */
/* Out : Nothing */
/* Description: Appends to FileName. Writes ASCII string */
/***************************************************************************/
void FileAppendString(char *FileName, char *AppendString)
{
FILE *fptr;
if ((fptr = fopen( FileName, "a+t" )) == NULL )
return; /* or error handling */

fputs(AppendString, fptr);
fflush(fptr);
fclose(fptr);
}
/***************************************************************************/
/* Function : Write string to output buffer */
/* In : option to force output, pointer to string */
/* Out : Nothing */
/* Description: Appends character string to end of null-terminated buffer */
/* If forced or buffer-full, stream buffer to file */
/***************************************************************************/
void WriteToDiskBuffer(int Forced, char *Data)
{
int StringLength;
int FlushNow = 0;

if (Data != NULL)
strcat(ReportDiskBuffer, Data);

StringLength = strlen(ReportDiskBuffer);
if ((Forced > 0) || (StringLength > (MaxReportBufferStrings * MaxLengthReportString))) FlushNow = 1;

if (FlushNow > 0)
{
if (StringLength > 0)
{
FileAppendString(ExportFileName, ReportDiskBuffer);
ReportDiskBuffer[0] = '\0';
}
}
}
void FlushDiskBuffer(void)
{
WriteToDiskBuffer(1,NULL);
}
0 Kudos
Message 2 of 8
(4,424 Views)
Hi.

In my project I need to write an array to a file.
I now 2 ways to do that:

1. Using the Write function who takes every line from my buffer and puts into the file.
2. Using the arraytofile function that saves the hole array to the file at once.


Now, I used the first method becose the second one translate the data into aschi format, but I badly
need to use the second one becose it saves time.

Example:

Using the first mettod in the file appairs the line:
hello
Using the second mettod the line is:
104 101 108 108 111

Thanks.
0 Kudos
Message 3 of 8
(4,421 Views)
ArrayToFile works only with numeric arrays.

To save an aray of strings to a file e re-read from it, you can use fread and fwrite. Supposing [s] is your array of [ns] strings each of them with lenght [arrayitemlen], to write to the file use:
fwrite ((char *)&s, arrayitemlen, ns, fileHandle);
This stores the whole array to disk at once.
Similarly, to read from the file use:
fread ((char *)&s, arrayitemlen, ns, fileHandle);

This works even with array of user-defined elements organized in structs.


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 4 of 8
(4,416 Views)
Hi.

The fwrite function works but after it writes into the file I find a lot of NULL caracters in the file. How can I get rid of them?

Thanks.
0 Kudos
Message 5 of 8
(4,392 Views)
Hello Tmaxial,

You probably are receiving extra characters in your output from fwrite since you are defining a standard size for all the elements in your string array. Therefore, if certain strings are shorter than the number you input, you might see junk characters in these spots. Instead, you can use the fputs function.
This function will write a string to an output stream. You can call this function in a loop to print each string element in your array. Of course, with this method, you can introduce significant overhead if you are traversing through a large array.


Thanks.
Wendy L
LabWindows/CVI Developer Newsletter
Message 6 of 8
(4,372 Views)
However you can limit the (slow) overhead by collecting the strings from multiple data before accessing the disk. This is shown in one of the first replies. That example shows that an ASCII string is created for each array element with the sprintf-function, and that these strings are catenated in a XX kByte character buffer. This buffer will be without the excess NULL characters. When the buffer is almost full, it uses the fputs-function to stream the content to disk.

Succes, Jos
0 Kudos
Message 7 of 8
(4,364 Views)
It's true that space after string end is filled with NULL or (some time) with garbage characters. This effectively depends on using fixed-lenght strings. In any case, strings are correctly terminated with a single NULL caracter, so you won't get garbage reading back your file.

Moreover, the fixed-lenght assumption generates a easier and faster read back from the array: in case you need to retrieve the i-th element of the array, you can move the file pointer i * SizeOfElement bytes away from start of file and read directly from the stream.
In case you compact strings to the shortest lenght, you will need to add some more lines of code to retrieve your data since each element does not take a fixed space on disk.

Resuming, here you have several methods to use for your problem: it's your application that will drive you to select the one which best fulfils your needs.

Just my 2c.


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 8 of 8
(4,352 Views)