Есть некоторые проблемы с strtok (), перечисленные здесь: http://benpfaff.org/writings/clc/strtok.html
Следовательно, лучше избегать strtok.
Теперь рассмотрим строку, содержащую пустое поле, следующим образом:
char myCSVString[101] = "-1.4,2.6,,-0.24,1.26"; // specify input here
Вы можете использовать простую функцию, чтобы иметь возможность преобразовать String в формате CSV, чтобы прочитать их в float Array:
int strCSV2Float(float *strFloatArray , char *myCSVStringing , char delim);
Мы указали разделитель здесь как запятую. Он работает с другим разделителем символов.
Пожалуйста, используйте следующее:
#include
#include
int strCSV2Float(float *strFloatArray , char *myCSVStringing , char delim);
void main()
{
char myCSVString[101] = "-1.4,2.6,,-0.24,1.26"; // specify input here
float floatArr[10]; // specify size of float array here
int totalValues = 0;
char myDelim = ','; // specify delimiter here
printf("myCSVString == %s \n",&myCSVString[0]);
totalValues = strCSV2Float(&floatArr[0] , &myCSVString[0], myDelim); // call the function here
int floatValueCount = 0;
for (floatValueCount = 0 ; floatValueCount < totalValues ; floatValueCount++)
{
printf("floatArr[%d] = %f\n",floatValueCount , floatArr[floatValueCount]);
}
}
int strCSV2Float(float *strFloatArray , char *myCSVStringing , char delim)
{
int strLen = 0;
int commaCount =0; // count the number of commas
int commaCountOld =0; // count the number of commas
int wordEndChar = 0;
int wordStartChar = -1;
int wordLength =0;
for(strLen=0; myCSVStringing[strLen] != '\0'; strLen++) // first get the string length
{
if ( (myCSVStringing[strLen] == delim) || ( myCSVStringing[strLen+1] == '\0' ))
{
commaCount++;
wordEndChar = strLen;
}
if ( (commaCount - commaCountOld) > 0 )
{
int aIter =0;
wordLength = (wordEndChar - wordStartChar);
char word[55] = "";
for (aIter = 0; aIter < wordLength; aIter++)
{
word[aIter] = myCSVStringing[strLen-wordLength+aIter+1];
}
if (word[aIter-1] == delim)
word[aIter-1] = '\0';
// printf("\n");
word[wordLength] = '\0';
strFloatArray[commaCount-1] = atof(&word[0]);
wordLength = 0;
wordStartChar = wordEndChar;
commaCountOld = commaCount;
}
}
return commaCount;
}
Выход выглядит следующим образом:
myCSVString == -1.4,2.6,,-0.24,1.26
floatArr[0] = -1.400000
floatArr[1] = 2.600000
floatArr[2] = 0.000000
floatArr[3] = -0.240000
floatArr[4] = 1.260000
Наконец, это работает отлично!
Спасибо Гордон Линофф!
SELECT ex.id_exportador,
exp.exportador,
ex.id_producto, pd.producto,
SUM((CASE WHEN (s.fechasalida >= $P{fechaInicial} AND s.fechasalida <= $P{fechaFinal}) THEN val.bultos ELSE 0 END)) AS b1,
SUM((CASE WHEN (s.fechasalida >= $P{anteriorInicial} AND s.fechasalida <= $P{anteriorFinal}) THEN val.bultos ELSE 0 END)) AS b2,
SUM((CASE WHEN (s.fechasalida >= $P{fechaInicial} AND s.fechasalida <= $P{fechaFinal}) THEN val.kg_brutos ELSE 0 END)) AS kgb1,
SUM((CASE WHEN (s.fechasalida >= $P{anteriorInicial} AND s.fechasalida <= $P{anteriorFinal}) THEN val.kg_brutos ELSE 0 END)) AS kgb2,
SUM((CASE WHEN (s.fechasalida >= $P{fechaInicial} AND s.fechasalida <= $P{fechaFinal}) THEN val.kg_netos ELSE 0 END)) AS kgn1,
SUM((CASE WHEN (s.fechasalida >= $P{anteriorInicial} AND s.fechasalida <= $P{anteriorFinal}) THEN val.kg_netos ELSE 0 END)) AS kgn2,
SUM((CASE WHEN (s.fechasalida >= $P{fechaInicial} AND s.fechasalida <= $P{fechaFinal}) THEN val.palets ELSE 0 END)) AS pal1,
SUM((CASE WHEN (s.fechasalida >= $P{anteriorInicial} AND s.fechasalida <= $P{anteriorFinal}) THEN val.palets ELSE 0 END)) AS pal2,
SUM((CASE WHEN (s.fechasalida >= $P{fechaInicial} AND s.fechasalida <= $P{fechaFinal}) THEN val.europalets ELSE 0 END)) AS epal1,
SUM((CASE WHEN (s.fechasalida >= $P{anteriorInicial} AND s.fechasalida <= $P{anteriorFinal}) THEN val.europalets ELSE 0 END)) AS epal2
FROM servicios s
LEFT JOIN expedientes ex
ON s.codservicio = ex.codservicio
LEFT JOIN exportadores exp
ON ex.id_exportador = exp.id_exportador
LEFT JOIN vales val
ON ex.id_expediente = val.id_expediente
LEFT JOIN productos pd
ON ex.id_producto = pd.id_producto
WHERE ((s.fechasalida >= $P{fechaInicial} AND s.fechasalida <= $P{fechaFinal}) OR (s.fechasalida >= $P{anteriorInicial} AND s.fechasalida <= $P{anteriorFinal})) AND ex.id_exportador IS NOT NULL
GROUP BY ex.id_exportador, ex.id_producto