LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Read CSV file with blank cells

Hi,

 

I want to read CSV file contents to a 2D array. My CSV file has some blank cells in between in some columns. When i am trying to read csv using fgets(), it is replacing the blank cells with the one from the adjacent cells.

 

For example, if 1st row, 1st column is empty, while reading it is replacing the 1st row 1st column value with 1st row 2nd column cell. But i am not expecting in that way. It should show in the array like that particular element is blank.

 

I have seen a similar post in Labview board. Can any one suggest any possible solution to do this in CVI.

 

Thanks,

Haari

 

0 Kudos
Message 1 of 10
(4,875 Views)

Hi,

 

instead of fgets use the functions fscanf or ScanFile.

0 Kudos
Message 2 of 10
(4,867 Views)

Hi,

 

I have an issue with fscanf(), it stops reading the row contents when it encounters a white space. For ScanFile() also, there is a limit to read the row contents. when i am trying to read with ScanFile(), its reading only part of the row contents....not sure why......

 

Thanks.

Haari

0 Kudos
Message 3 of 10
(4,865 Views)

I would assume that this is due to an inappropriate format specifier but without details about your data and code it's hard to tell. Smiley Wink

0 Kudos
Message 4 of 10
(4,858 Views)

Hi,

 

I have attached my sample.csv file. I want my 2D output array to have the following data:

 

row 0 should have {"x","y","z"}

row 1 should have {"","1","2"}

row 2 should have {"a","b",""}

 

Please can anyone help me in acheiving this.

 

Regards,

Haari

 

 

 

0 Kudos
Message 5 of 10
(4,837 Views)

Forgot to send the attachment.

0 Kudos
Message 6 of 10
(4,836 Views)

You also forgot to add your source code...

 

What about:

 

ScanFile ( handle, "%s>%s[yt44x]%s[yt44x]%s", ro1,ro2,ro3);

0 Kudos
Message 7 of 10
(4,825 Views)

Hi,

 

Currently i am using fgets() to read a line from CSV file. while reading it is replacing the blank cells with adjacent cell values which i dont want. I tried with ScanFile() which 'wolfgang' has suggested, i am having one issue with that...

 

if my .csv has 14 columns and if my first row has elements upto 10 columns, remaining all are blank. in that case ScanFile() is appending the next row elements to this data.

 

Can any one help me in this regard?

 

Regards,

Haari

 

 

 

 

 

 

0 Kudos
Message 8 of 10
(4,792 Views)

The example I have provided was designed to work for (and has been tested with) your data set of three columns - you are right that it does not work for 14 columns...Smiley Frustrated

 

You could consider adapting it yourself by looking at the [ rep ] format specifier which indicates how many times the format repeats with respect to the arguments

0 Kudos
Message 9 of 10
(4,784 Views)

I would use fgets or ReadLine to read the file line by line and extract data from the line using Scan with the formatting string posted by Wolfgang some post earlier. This would prevent to consider data from subsequent lines as being part of the previous one.



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 10 of 10
(4,776 Views)