reading a csv file stored on master
JOHNBONZ
Posts: 99
If I have a CSV file like so:
Zone Room
Zone Name
ID
Kitchen
Island
2
`
Den
Table Lamp
3
Outdoor
Garage Inside
5
Foyer
Chandieler
6
Foyer
Table Lamp
7
Outdoor
Side Garage Dr
10
Great Room
Pictures
14
Great Room
Sconces
15
Great Room
Table Lamp
16
Outdoor
Coach
17
Outdoor
Deck
18
Kitchen
Kitchen Hall
22
with 3 columns and I want to bring into netlinks parse the data and store in a structure like below.
STRUCTURE _sLutronDev
{
integer ID; //Device ID
char cNodeName[16]; //Device Name
integer nLoc; //Device Location
}
I have read text files stored on Master before, but it was only one column (and not a CSV file) as I want to read a file that has 3 columns per line.
here is how I read a text file that stored names of cable channels: for example
logo_a&e.png
logo_abc family.jpg
logo_abc-family.png
.
.
.
non_volatile char aIconCableNames[nTotalIcons][64];
slFILE = FILE_OPEN('_tv_icon_list3.txt', 1); // open file, read only
if(slFILE > 0) // file open successful?
{
aIconCableNames = clearNames;
for(i=1; i<=nTotalIcons; i++)
{
siCNT = file_read_line(slFILE, cLINE, 128); // read a line from the file "64 buffer max"
if(siCNT > 0)
aIconCableNames = cLINE;
else
break;
}
file_close(slFILE); // close the file
}
then I would store each value in an alpha Array -- aIconCableNames
So to do this but with 3 columns per row ---
Does anyone know of a quick way to do this?
Zone Room
Zone Name
ID
Kitchen
Island
2
`
Den
Table Lamp
3
Outdoor
Garage Inside
5
Foyer
Chandieler
6
Foyer
Table Lamp
7
Outdoor
Side Garage Dr
10
Great Room
Pictures
14
Great Room
Sconces
15
Great Room
Table Lamp
16
Outdoor
Coach
17
Outdoor
Deck
18
Kitchen
Kitchen Hall
22
with 3 columns and I want to bring into netlinks parse the data and store in a structure like below.
STRUCTURE _sLutronDev
{
integer ID; //Device ID
char cNodeName[16]; //Device Name
integer nLoc; //Device Location
}
I have read text files stored on Master before, but it was only one column (and not a CSV file) as I want to read a file that has 3 columns per line.
here is how I read a text file that stored names of cable channels: for example
logo_a&e.png
logo_abc family.jpg
logo_abc-family.png
.
.
.
non_volatile char aIconCableNames[nTotalIcons][64];
slFILE = FILE_OPEN('_tv_icon_list3.txt', 1); // open file, read only
if(slFILE > 0) // file open successful?
{
aIconCableNames = clearNames;
for(i=1; i<=nTotalIcons; i++)
{
siCNT = file_read_line(slFILE, cLINE, 128); // read a line from the file "64 buffer max"
if(siCNT > 0)
aIconCableNames = cLINE;
else
break;
}
file_close(slFILE); // close the file
}
then I would store each value in an alpha Array -- aIconCableNames
So to do this but with 3 columns per row ---
Does anyone know of a quick way to do this?
0
Comments
A CSV file is simply a text file that uses commas to delimit each column.
If you change the extension from .CSV to .TXT and then open up the file you will see the following text:
Zone Room,Zone Name,ID
Kitchen,Island,2
Den,Table Lamp,3
Outdoor,Garage Inside,5
Foyer,Chandieler,6
Foyer,Table Lamp,7
Outdoor,Side Garage Dr,10
Great Room,Pictures,14
Great Room,Sconces,15
Great Room,Table Lamp,16
Outdoor,Coach,17
Outdoor,Deck,18
Kitchen,Kitchen Hall,22
You read a CSV file the same way as any other TXT file (line by line) and the commas in each line will tell you where to spit the string and where to put the data in your structure.
A couple of things to keep in mind:
1) Don't use a comma in any of your fields.
2) If you are using one of the lines in the file as a header (or other lines for comments) then precede the first word in the first column with a semi-colon (or some other flag) that will tell your code to ignore that line. So your first row will look like this:
;Zone Room, Zone Name, ID.
When you read in each line look to see if that line starts with a semi-colon and if it does throw it away and move to the next line.
Have fun.
thanks Joe!
Glad you got things working.
Just as a point of clarification you don't actually need to change the extension of the file if you don't want to. It makes no difference when you read the file in Netlinx, the results will be the same.
When I suggested changing the extension from .CSV to .TXT it was so a text editor would open up when you clicked on it instead of opening up as a spreadsheet. When opened in a text editor you can see the commas that were inserted when you originally created and saved the file as a .CSV.
In either case (csv or txt) the contents of the file are exactly the same and the contents are what matter when you read the file in Netlinx.