| | |
Reading CSV in VC++
![]() |
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
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]);
}
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]);
}
>>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
and the while loop is incorrect. use of eof() is unpredictable.
>> 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
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
Finally, you can use loops to make the program more efficient. You can replace all those getline()s with just this one.
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)
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)
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)
int r = 0, c = 0; while( input_file.getline(line, sizeof(line), ',') > 0) { matrix_points[r][c] = (char)atoi(line); ++c; if( c == 8) { r++; c = 0; } }
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
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)
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)
int r = 0, c = 0; while( input_file.getline(line, sizeof(line), ',') > 0) { matrix_points[r][c] = (char)atoi(line); ++c; if( c == 8) { r++; c = 0; } }
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
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.
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.
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.
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)
const int ItemsPerRow = 8; typedef std::vector<string> C; int _tmain(int argc, _TCHAR* argv[]) { std::vector<C> matrix_points; C words; std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv"); int c = 0; std::string line; while( getline(input_file,line) ) { // now bust the line up into individual words while(line != "") { int pos = line.find(' '); if(pos > 0) { words.push_back(line.substr(0,pos)); line = line.substr(pos+1); } else { words.push_back(line); line = ""; } } matrix_points.push_back(words); words.erase(words.begin(),words.end()); } return 0; }
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
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.
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
The code I posted will still work, just replace the space in find() with a comma
C++ Syntax (Toggle Plain Text)
int pos = line.find(',');
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Reading in a *.csv file and loading the data into an Array (Java)
- Reading in certain columns from CSV files into array C++ (C++)
- CSV file (C)
- Reading CSV file into a ADO recordset (ASP.NET)
- Dynamic Array, Writing to CSV, Floating Point ?'s (C)
- Need Help Reading a csv file created from MSExcel (C)
Other Threads in the C++ Forum
- Previous Thread: how to share data between two simultaneously running programs?
- Next Thread: Help with C++ program find a way in a maze
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






