944,093 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17838
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 11th, 2006
0

Reading CSV in VC++

Expand Post »
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]);
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NarenderKumarP is offline Offline
14 posts
since Apr 2006
Apr 12th, 2006
0

Re: Reading CSV in VC++

>>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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,957 posts
since Aug 2005
Apr 12th, 2006
0

Re: Reading CSV in VC++

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


Quote 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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NarenderKumarP is offline Offline
14 posts
since Apr 2006
Apr 12th, 2006
0

Re: Reading CSV in VC++

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NarenderKumarP is offline Offline
14 posts
since Apr 2006
Apr 12th, 2006
0

Re: Reading CSV in VC++

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 12th, 2006
0

Re: Reading CSV in VC++

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NarenderKumarP is offline Offline
14 posts
since Apr 2006
Apr 12th, 2006
0

Re: Reading CSV in VC++

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.

C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,957 posts
since Aug 2005
Apr 12th, 2006
0

Re: Reading CSV in VC++

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.



Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NarenderKumarP is offline Offline
14 posts
since Apr 2006
Apr 13th, 2006
0

Re: Reading CSV in VC++

The code I posted will still work, just replace the space in find() with a comma
C++ Syntax (Toggle Plain Text)
  1. int pos = line.find(',');
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,957 posts
since Aug 2005
Apr 13th, 2006
0

Re: Reading CSV in VC++

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NarenderKumarP is offline Offline
14 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to share data between two simultaneously running programs?
Next Thread in C++ Forum Timeline: big help with alil program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC