10-15-2012 05:21 AM
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
10-15-2012 05:41 AM
Hi,
instead of fgets use the functions fscanf or ScanFile.
10-15-2012 06:11 AM
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
10-15-2012 07:45 AM
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.
10-16-2012 09:20 AM
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
10-16-2012 09:20 AM
Forgot to send the attachment.
10-16-2012 10:33 AM
You also forgot to add your source code...
What about:
ScanFile ( handle, "%s>%s[yt44x]%s[yt44x]%s", ro1,ro2,ro3);
10-21-2012 01:31 AM
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
10-21-2012 03:34 AM
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...
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
10-21-2012 06:47 AM
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.