Home AMX General Discussion

reading a csv file stored on master

JOHNBONZJOHNBONZ Junior MemberPosts: 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?

Comments

  • Joe HebertJoe Hebert Junior Member Posts: 2,158
    JOHNBONZ wrote: »
    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.

    Does anyone know of a quick way to do this?

    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.


  • infoscreeninfoscreen Junior Member Posts: 14
    Of course the TXT file read is easy. But I'm remember one other mode. Please look the AMX i!-DataBase_Plus module ! Very easy use and database connect.
  • HARMAN_icraigieHARMAN_icraigie Technical Trainer II, Harman Professional University Posts: 536
    FYI the SNAPI,axi function CHAR[] DuetParseCmdParam(CHAR cCmd[]) is a csv parser.
  • JOHNBONZJOHNBONZ Junior Member Posts: 99
    yes changed to file type to .TXT and worked like a charm as I have AMX parse thru the file and I load the data into structures without manually doing it
    thanks Joe!
  • Joe HebertJoe Hebert Junior Member Posts: 2,158
    JOHNBONZ wrote: »
    yes changed to file type to .TXT and worked like a charm as I have AMX parse thru the file and I load the data into structures without manually doing it
    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.

Sign In or Register to comment.