Reading CSV in VC++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 14
Reputation: NarenderKumarP is an unknown quantity at this point 
Solved Threads: 0
NarenderKumarP NarenderKumarP is offline Offline
Newbie Poster

Reading CSV in VC++

 
0
  #1
Apr 11th, 2006
Hi All,
Following code gives me an error when I use text field in CSV file instead of float or integer fields.

matrix_points[cnt][4] = atoi(line);
May be the problem with above line. Please see the below code


char matrix_points[6][8];


std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv");
int cnt(0);
printf("initial valie of cnt = %d\n",cnt);
char buffer[256];
char line[255];


input_file.clear();
input_file.seekg(0);

while(!(input_file.eof()))
{
input_file.getline(line, sizeof(buffer), ',');
matrix_points[cnt][0] = line;
//printf(" %s", line);

input_file.getline(line, sizeof(buffer), ',');
matrix_points[cnt][1] = atoi(line);
//printf(" %s", line);


input_file.getline(line, sizeof(buffer), ',');
matrix_points[cnt][2] = atoi(line);
//printf(" %s", line);

input_file.getline(line, sizeof(buffer), ',');
matrix_points[cnt][3] = atoi(line);
//printf(" %s", line);

input_file.getline(line, sizeof(buffer), ',');
matrix_points[cnt][4] = atoi(line);
//printf(" %s", line);

input_file.getline(line, sizeof(buffer), ',');
matrix_points[cnt][5] = atoi(line);
//printf(" %s", line);

input_file.getline(line, sizeof(buffer), ',');
matrix_points[cnt][6] = atoi(line);
//printf(" %s", line);

input_file.getline(line, sizeof(buffer), ',');
matrix_points[cnt][7] = atoi(line);
//printf(" %s", line);
cnt++;
}

for (int i=0; i<cnt; i++){

printf("%s", matrix_points[i][0]);
printf(" %s", matrix_points[i][1]);
printf(" %s", matrix_points[i][2]);
printf(" %s", matrix_points[i][3]);
printf(" %s", matrix_points[i][4]);
printf(" %s", matrix_points[i][5]);
printf(" %s", matrix_points[i][6]);
printf(" %s\n", matrix_points[i][7]);
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Reading CSV in VC++

 
0
  #2
Apr 12th, 2006
>>Following code gives me an error when I use text field in CSV file instead of float or integer fields

what compiler? post the errors. The compiler should give you warnings that it is converting int to char and may lose data.

what does the csv file look like -- can you post the first two or three lines? Are the values between -127 and 126 because that is the range of signed char. If the csv file contains larger numbers then you will have to redeclare the matrix as a 2d array of ints, not chars
  1. int matrix_points[6][8];

and the while loop is incorrect. use of eof() is unpredictable.
// while(!(input_file.eof()))
//{
while( input_file.getline(line, sizeof(line), ',') > 0)
{
   // blabla
}

>> printf("%s", matrix_points[i][0]);
all these lines are incorrect -- %s expects a null-terminated string, and what you are passing is just one character. They should look like this
  1. printf("%s", matrix_points[i]);

This assumes that the array is null-terminated strings. If not, then %s will not work -- probably crash your program. In that case, change %s to %c as your original post
 printf("%c", matrix_points[i][0]);

Finally, you can use loops to make the program more efficient. You can replace all those getline()s with just this one.

  1. int r = 0, c = 0;
  2. while( input_file.getline(line, sizeof(line), ',') > 0)
  3. {
  4. matrix_points[r][c] = (char)atoi(line);
  5. ++c;
  6. if( c == 8)
  7. {
  8. r++;
  9. c = 0;
  10. }
  11. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 14
Reputation: NarenderKumarP is an unknown quantity at this point 
Solved Threads: 0
NarenderKumarP NarenderKumarP is offline Offline
Newbie Poster

Re: Reading CSV in VC++

 
0
  #3
Apr 12th, 2006
Hi Dragon,
This is how the csv file looks

ECU ID FuncID TstID TstStatus ResType ResCode codeTxt deftText
* * * * * * Default Default
ECU_MODEL_KWP_ECCS 4 WS-2-1 * * * 1 Emergency stop
ECU_MODEL_KWP_ABS 3 AT-2-01 * * * 2 Stop test
...
...
As of now I have 6 rows and 8 columns. In future rows may increase and column might also change. So how can we make it dynamic. But first I want my static one to work. Would appreciate your support in this regard.

Narender

Its a VC++ compiler and gives the following message

error C2440: '=' : cannot convert from 'char [255]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast


Originally Posted by Ancient Dragon
>>Following code gives me an error when I use text field in CSV file instead of float or integer fields

what compiler? post the errors. The compiler should give you warnings that it is converting int to char and may lose data.

what does the csv file look like -- can you post the first two or three lines? Are the values between -127 and 126 because that is the range of signed char. If the csv file contains larger numbers then you will have to redeclare the matrix as a 2d array of ints, not chars
  1. int matrix_points[6][8];

and the while loop is incorrect. use of eof() is unpredictable.
// while(!(input_file.eof()))
//{
while( input_file.getline(line, sizeof(line), ',') > 0)
{
   // blabla
}

>> printf("%s", matrix_points[i][0]);
all these lines are incorrect -- %s expects a null-terminated string, and what you are passing is just one character. They should look like this
  1. printf("%s", matrix_points[i]);

This assumes that the array is null-terminated strings. If not, then %s will not work -- probably crash your program. In that case, change %s to %c as your original post
 printf("%c", matrix_points[i][0]);

Finally, you can use loops to make the program more efficient. You can replace all those getline()s with just this one.

  1. int r = 0, c = 0;
  2. while( input_file.getline(line, sizeof(line), ',') > 0)
  3. {
  4. matrix_points[r][c] = (char)atoi(line);
  5. ++c;
  6. if( c == 8)
  7. {
  8. r++;
  9. c = 0;
  10. }
  11. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 14
Reputation: NarenderKumarP is an unknown quantity at this point 
Solved Threads: 0
NarenderKumarP NarenderKumarP is offline Offline
Newbie Poster

Re: Reading CSV in VC++

 
0
  #4
Apr 12th, 2006
This is the format

ECU ID Function ID Test ID Test Status Response Type Response Code codeText defaultText
* * * * * * Default Default
ECU_MODEL_KWP_ECCS 4 WS-2-1 * * * 1 Emergency stop
ECU_MODEL_KWP_ABS 3 AT-2-01 * * * 2 Stop test
ECU_MODEL_CAN_ABS 4 WS-2-1 * * * 3 Adjustment finished
ECU_MODEL_CAN_LDW 4 WS-1 * * * 4 Stop
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reading CSV in VC++

 
0
  #5
Apr 12th, 2006
Ah, I see.
It's that new style of CSV file without any comma's in it :rolleyes:
Perhaps the fields are separated with a tab character instead.

> char matrix_points[6][8];
Perhaps
std::string matrix_points[6][8];
would be better for storing things.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 14
Reputation: NarenderKumarP is an unknown quantity at this point 
Solved Threads: 0
NarenderKumarP NarenderKumarP is offline Offline
Newbie Poster

Re: Reading CSV in VC++

 
0
  #6
Apr 12th, 2006
I am facing a problem while assigning the line value to two dimensional array.
So how should I assign the value of line variable to the two dimensional array as follows

Basically I want to read all the values in the matrix. Based on these values I have to conditionally replace the values in my application. That is for a given row I want to find the value of particular field in the row and then extract the the value of other field in the same row using the matrix(2-D array). Here is my

int matrix_points[6][8];

std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv");

char line[255];

input_file.clear();
input_file.seekg(0);
int r = 0, c = 0;

while( input_file.getline(line, sizeof(line), ','))
{
matrix_points[r][c] = (char)atoi(line);
++c;
if( c == 8)
{
r++;
c = 0;
}
}

//Retrieving Value

for (int i=0; i<r; i++)
for(int j=0; j<c; j++)
{
printf("%s", matrix_points[i]);
}
Error comes when I assign the value of line to matrix array and when I try to display the value of this matrix with specific dimension. E.g matrix_points[1][7]

So basically the problem is : storing the value in matrix_points and extracting the values

No error is displayed but I don’t get the expected output.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Reading CSV in VC++

 
0
  #7
Apr 12th, 2006
Its obvious from the fle contents that atoi() won't work because the file contains a mixture of text and numbers. And since that is NOT a comma-separated file attempting to read up to the comma will fail. As Salem mentioned the columns must be separated by tabs or just simply spaces.

here is a way to do it without any knowledge of the number of words on a line. matrix_points must be defined as a 2d array of strings -- you have it declared as a 2d array of characters. You can't store an entire string in just one character.

  1. const int ItemsPerRow = 8;
  2. typedef std::vector<string> C;
  3. int _tmain(int argc, _TCHAR* argv[])
  4. {
  5. std::vector<C> matrix_points;
  6. C words;
  7. std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv");
  8. int c = 0;
  9. std::string line;
  10. while( getline(input_file,line) )
  11. {
  12. // now bust the line up into individual words
  13. while(line != "")
  14. {
  15. int pos = line.find(' ');
  16. if(pos > 0)
  17. {
  18. words.push_back(line.substr(0,pos));
  19. line = line.substr(pos+1);
  20. }
  21. else
  22. {
  23. words.push_back(line);
  24. line = "";
  25. }
  26. }
  27. matrix_points.push_back(words);
  28. words.erase(words.begin(),words.end());
  29. }
  30. return 0;
  31. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 14
Reputation: NarenderKumarP is an unknown quantity at this point 
Solved Threads: 0
NarenderKumarP NarenderKumarP is offline Offline
Newbie Poster

Re: Reading CSV in VC++

 
0
  #8
Apr 12th, 2006
Actually yesterday I just copied the content from csv file as it is and therefore looked to be tab/space separated file. Following is the correct format
ECU ID,Function ID,Test ID,Test Status,Response Type,Response Code,codeText,defaultText
*,*,*,*,*,*,Default,Default
ECU_MODEL_KWP_ECCS,4,WS-2-1,*,*,*,1,Emergency stop
ECU_MODEL_KWP_ABS,3,AT-2-01,*,*,*,2,Stop test
ECU_MODEL_CAN_ABS,4,WS-2-1,*,*,*,3,Adjustment finished
ECU_MODEL_CAN_LDW,4,WS-1,*,*,*,4,Stop

Here how do I know the end of line.
I want to read all the value in 2D array and then extract conditionally in the for loop.



Originally Posted by NarenderKumarP
Hi Dragon,
This is how the csv file looks

ECU ID FuncID TstID TstStatus ResType ResCode codeTxt deftText
* * * * * * Default Default
ECU_MODEL_KWP_ECCS 4 WS-2-1 * * * 1 Emergency stop
ECU_MODEL_KWP_ABS 3 AT-2-01 * * * 2 Stop test
...
...
As of now I have 6 rows and 8 columns. In future rows may increase and column might also change. So how can we make it dynamic. But first I want my static one to work. Would appreciate your support in this regard.

Narender

Its a VC++ compiler and gives the following message

error C2440: '=' : cannot convert from 'char [255]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Reading CSV in VC++

 
0
  #9
Apr 13th, 2006
The code I posted will still work, just replace the space in find() with a comma
  1. int pos = line.find(',');
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 14
Reputation: NarenderKumarP is an unknown quantity at this point 
Solved Threads: 0
NarenderKumarP NarenderKumarP is offline Offline
Newbie Poster

Re: Reading CSV in VC++

 
0
  #10
Apr 13th, 2006
This(following) is the code I am using which is almost same as yours. Certain c functions are not working in VC++ like push_back, substr etc. Still output is always the first field, i.e. ECU ID in the format above.
Can you please send me the code more in the line of VC++(if not C++ is also OK)

QString linecol;

while(input_file.getline(line, sizeof(line), '\n')) // to read lines
{
while(line != "")
{
linecol = line;
int pos = linecol.find(',');
field = linecol.left(pos);
if(pos > 0)
{

linecol = linecol.left(pos); // substr function does not work in VC++.
matrix_points[r][c] = linecol;
printf(matrix_points[r][c]);
linecol = linecol.left(pos+1);
++c;
}
else
{
matrix_points[r][c] = line;
linecol = "";
c = 0;
r++;
}

}

}

Thanks & Regards,
Narender
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC