LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

The FileToArray function

Hi.

I'm stuck at FileToArray fuction.

My text file has the same number of characters for each line and it looks like this:


6 jsndje 10:28
6 dfghdf 10:52
8 dsfbbs 10:52
111 ertyew 10:52
0 wertwe 10:52
39 dfghdw 10:52
16 wergw 10:52
26 wergw 10:52
0 wergsg 10:53
111 hretee 10:53
111 dfgnng 10:53
111 ertwrg 10:53
111 dfgnng 10:53
111 dfgnxc 10:53
111 fhmfhm 10:53
111 hgtj,m 10:53
111 fghmdd 10:53
111 gfhmfr 10:53

My program lines are:

char MyArray[500][22];

stat = fopen (myfile, "a");
NumberOfCharacters = ftell (stat);
GroupNumber=NumberOfCharacters/23;


status = FileToArray (DirectorBD, MyArray, VAL_CHAR, NumberOfCharacters, GroupNumber,
VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_ROWS, VAL_ASCII);

Where I make the mistake? Becose it doesn'n work. I need MyArray to contain the data from file.


Thanks.
0 Kudos
Message 1 of 4
(3,532 Views)
Theres an example from thier site using this function so it may give some clues...
http://sine.ni.com/apps/we/niepd_web_display.DISPLAY_EPD4?p_guid=B45EACE3DF5556A4E034080020E74861&p_node=DZ52146&p_submitted=N&p_rank=&p_answer=&p_source=External

One thing that looks wrong (to me at least) is:
char MyArray[500][22]; // declaired as 22 chars long each
GroupNumber=NumberOfCharacters/23; // divide by 23?? should be 22
0 Kudos
Message 2 of 4
(3,513 Views)
Hi.

Thanks for the clues, but my problem is that my file has numbers and strings, and I can't load them into an array
that has only a type of data. I tryed first with an char array but the function FileToArray can't see the entire line from my file (it loads characters until it reachs the space character " " and than it jumps at the next line).
I tryed with an integer array but the function loads only the numbers from a line.
How can I define an array that contains my entire file? Or maybi exists another function that loads characters from a file to an array.

Thanks.
0 Kudos
Message 3 of 4
(3,505 Views)
Hi tmaxial.

You could try using the Scan() function to parse each line of the file:

#define MAX_LINE_LENGTH 80

int myData[500]; // change to double[] if you need to (also change the Scan() call)
int i=0, iTemp, hour, minute, dataItems;
char lineBuffer[MAX_LINE_LENGTH];


handle = OpenFile (myfile, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);

bytesRead = ReadLine (handle, lineBuffer, MAX_LINE_LENGTH - 1);
while ((bytesRead >= 0) && (i < 500)) // not end of file or error
{
// Skip blank lines
if (!bytesRead)
continue;
items = Scan (lineBuffer, "%i %s[w6] %i:%i", &iTemp, dummy, &hour, &minute);
if (items < 0)
break;
if (items > 0)
{
myData[i++] = iTemp;
// Store string & other data if required...
}
bytesRead = ReadLine (handle, lineBuffer, MAX_LINE_LENGTH - 1);
}
dataItems = i;

Regards,
Colin.
Message 4 of 4
(3,490 Views)