| | |
Reading CSV in VC++
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
>> VC++
That is the name of a specific compiler, not c programming language. Of course it supports c++, vector and push_back. You are writing a c++ program, if you want to use vector c++ class as I illustrated you have to include <vector> header file.
>>QString linecol
QString is a non-standard c++ class that M$ apparently developed for their compilers. I haven't used managed c++. the strings I posted are std::string from <string> header file, and maybe that is why you couldn't get it to work.
>> matrix_points[r][c] = line;
I mentioned this before -- you can't do this because matric_points is a 2d array of characters, not a 2d array of pointers
how is line declared? if it is a char array, then the code above will not work. If it is QString or std::string it still will not work. You are mixing c and c++ -- attempting to use c++ techniques on C string, or attempting to use C techniques on C++ string class. But I don't know which it is because you didn't post enough code.
That is the name of a specific compiler, not c programming language. Of course it supports c++, vector and push_back. You are writing a c++ program, if you want to use vector c++ class as I illustrated you have to include <vector> header file.
>>QString linecol
QString is a non-standard c++ class that M$ apparently developed for their compilers. I haven't used managed c++. the strings I posted are std::string from <string> header file, and maybe that is why you couldn't get it to work.
>> matrix_points[r][c] = line;
I mentioned this before -- you can't do this because matric_points is a 2d array of characters, not a 2d array of pointers
C++ Syntax (Toggle Plain Text)
char *matrix_points[255][255]; // 2d array of pointers matrix_points[r][c] = new char[strlen(line)+1]; strcpy(matrix_points[r][c], line);
C++ Syntax (Toggle Plain Text)
while(input_file.getline(line, sizeof(line), '\n')) // to read lines { while(line != "")
how is line declared? if it is a char array, then the code above will not work. If it is QString or std::string it still will not work. You are mixing c and c++ -- attempting to use c++ techniques on C string, or attempting to use C techniques on C++ string class. But I don't know which it is because you didn't post enough code.
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
Hi,
Yeah line is character array. This is how the following code looks like. Can you please find the area with mistakes and correct it.
QString matrix_points[6][8];
char line[255];
std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv");
input_file.clear();
input_file.seekg(0);
int r = 0, c = 0;
QString linecol, field;
while(input_file.getline(line, sizeof(line), '\n')) // to read lines
{
while(line != "")
{
linecol = line;
//linecol = linecol.stripWhiteSpace();
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++;
}
}
}
Yeah line is character array. This is how the following code looks like. Can you please find the area with mistakes and correct it.
QString matrix_points[6][8];
char line[255];
std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv");
input_file.clear();
input_file.seekg(0);
int r = 0, c = 0;
QString linecol, field;
while(input_file.getline(line, sizeof(line), '\n')) // to read lines
{
while(line != "")
{
linecol = line;
//linecol = linecol.stripWhiteSpace();
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++;
}
}
}
while(input_file.getline(line, sizeof(line), '\n')) // to read lines
{
linecol = line;
while(linecol != "")
{
int pos = linecol.find(',');
field = linecol.left(pos);
,,,std::string.substr(...) is similar to QString's mid method.
http://doc.trolltech.com/3.3/qstring.html
I don't know why you are using those odd-ball, non-standard c++ classes, maybe the university requires them? But anyway, looks like ACE_WString has a constructor that takes const char*, so just pass the right QString method that makes that conversion for you. You'll have to look it up.
C++ Syntax (Toggle Plain Text)
QString str = "Hello World"; ACE_WString wstring(str.??());
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
This is what my whole code looks like. I am unable to locate the constructor which would help store into my codeText variable(ACE_WString) the value of matrix_points(QString). As you see the paramter in the function of my application requires me to pass ACE_WString type. Basically I want to assign QString type value to ACE_WString variable.
Follwoing is the error
Follwoing is the error
C++ Syntax (Toggle Plain Text)
C:\NDA\NissanNewApplication\Dev\CommonErrorManager\Src\CCommonErrorManager.cpp(193) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class QString' (or there is no acceptable conversion)
C++ Syntax (Toggle Plain Text)
int CCommonErrorManager::requestErrorHandling(ErrorEvent& ev, ACE_WString& codeText, ACE_WString& defaultText, int& functionID,ACE_WString& testID, int& testStat) { //std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv"); std::ifstream input_file("../../Src/ErrorMessageList.csv"); char line[255]; input_file.clear(); input_file.seekg(0); int r = 0, c = 0, colcnt = 0; QString linecol, field; while(input_file.getline(line, sizeof(line), '\n')) // to read lines { linecol = line; while(linecol != "") { //linecol = linecol.stripWhiteSpace(); int pos = linecol.find(','); if(pos > 0) { //linecol = linecol.left(pos); // substr function does not work in VC++. matrix_points[r][c] = linecol.mid(0,pos); //printf(matrix_points[r][c]); //printf(" "); linecol = linecol.mid(pos+1); c++; } else { matrix_points[r][c] = linecol; //printf(matrix_points[r][c]); linecol = ""; colcnt = c; c = 0; r++; printf("\n"); } } } //Retrieving Value ACE_WString wStr = codeText; ACE_TString tStr = ACE_Wide_To_Ascii(wStr.c_str()).char_rep(); for (int i=0; i<r; i++) if(matrix_points[i][0] == "ECU_MODEL_KWP_ABS") { //printf(matrix_points[i][3]+" " ); //printf(matrix_points[i][6]+" " ); //printf(matrix_points[i][7] + "\n"); <div class="bbquote"> <div class="tlc"><div class="tli">•</div></div> <div class="blc"><div class="bli">•</div></div> <div class="trc"><div class="tri">•</div></div> <div class="brc"><div class="bri">•</div></div> <blockquote style="font-size:11px">codeText = matrix_points[i][6];</blockquote> </div> //codeText (const char *matrix_points[i][6], ACE_Allocator *alloc = 0); } //Initializing the output parameters codeText & defaultText. Both of these need to be searched in the database. //codeText = L""; defaultText = L""; /* Acquiring Response Code ev.getEvent() -> Response code such as 21h,23h */ int rcode; rcode = ev.getEvent(); /* ev.getType -> Error Type */ if(ev.getType() == ErrorEvent::Unknown) { LOGI(ACE_TEXT("Error Type:[Unknown]\n")); /* Channel_Error_Notification -> No Response */ if(ev.getNewState() == ICommManager::CHANNEL_ERROR_NOTIFICATION) { LOGI(ACE_TEXT("Channel Error Notification....[No Response]")); m_pLCP->reLink(); //m_pLCP->ReTry(); return 2; } else if((ev.getNewState() == ICommManager::NACK_COMMAND) || (ev.getNewState() == ICommManager::NACK_EPI)) { switch(rcode) { LOGI(ACE_TEXT("Determining Response Code")); /* Negative Response 21h and 23h */ case 0x21 : case 0x23 : LOGI1(ACE_TEXT("The Response Code is [%x]"),rcode); //ev.getSource() -> For ECUID. if(ev.getSource() == "ECU_MODEL_CAN_SMKEY") //tStr<-->"ECU_MODEL_CAN_SMKEY" { LOGI(ACE_TEXT("Checking ECU Id....ECU_MODEL_CAN_SMKEY")); LOGI(ACE_TEXT("[ReLink] and [ReTry()] are called")); m_pLCP->reLink(); } else { LOGI(ACE_TEXT("Only [ReTry()] is called")); m_pLCP->retry(); } return 2; break; /* Negative Response 78h */ case 0x78: LOGI(ACE_TEXT("Negative Response Code : [78h]")); return 1; break; default: LOGI(ACE_TEXT("Response Code other than 21h,23h,78h")); if(ev.getSource() == "ECU_MODEL_CAN_SMKEY" && counter == 0) //tStr<-->"ECU_MODEL_CAN_SMKEY" { LOGI(ACE_TEXT("ECU Id: [ECU_MODEL_CAN_SMKEY]")); counter = counter + 1; m_pLCP->reLink(); return 2; } else { codeText = L"Default"; defaultText = L"Default"; LOGI(ACE_TEXT("Displaying Error in GUI")); /* set the counter to 0 */ counter = 0; /* Callback Function */ //m_pCB->showErrorScreen(); return 3; } break; } // end of switch } // end of else if of getNewState()=CHANNEL_ERROR_NOTIFICATION else // else of getNewState()=NACK_COMMAND OR NACK_EPI { codeText = L"Default"; defaultText = L"Default"; LOGI(ACE_TEXT("Unknown Error is encountered.")); return 3; } } // if getType() else // Else of getType() { codeText = L"Default"; defaultText = L"Default"; LOGI(ACE_TEXT("A problem has occured at hardware level")); return 3; } //return 3; } // end of Function
Last edited by Dave Sinkula; Apr 18th, 2006 at 11:59 pm.
Here is the version of ACE_WString whose second constructor takes char*. I have no way of knowing if that's the same one you are using.
QString has two methods that you may be interested in -- ascii() that returns char*, and unicode() that appears to make the ascii to unicode conversion for you.
I'm just sort of guessing about the above because I don't have either of those classes.
QString has two methods that you may be interested in -- ascii() that returns char*, and unicode() that appears to make the ascii to unicode conversion for you.
C++ Syntax (Toggle Plain Text)
QString str = "Hello"; ACE_WString w(str.ascii());
I'm just sort of guessing about the above because I don't have either of those classes.
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
Your dilema seems vague. I assume you have developed a 2 dimensional table of values like tableName[rowIndex][colIndex] and each column has a name/header/identifier. I further assume you want to be able to find a given column identified by it's name/header/identifier and be able to access the elements of the column to manipulate them in some fashion. If that's correct, then you can view the headers to be a row of data just like a row of values making up the body of the table itself and use a loop to search for the header/identifier you want. To do that you can hold the row index stable and vary the column index. Then to access all the values in the column you hold the column index stable and vary the row index.
•
•
Join Date: Apr 2006
Posts: 14
Reputation:
Solved Threads: 0
Hi Lerner,
Thanks! So far I have done the same. My next step is to match the value till couple of columns in a row and if 2 rows are almost similar the value for the row which matches more closer should be displayed.
A glimpse of my code
Thanks & Regards
Thanks! So far I have done the same. My next step is to match the value till couple of columns in a row and if 2 rows are almost similar the value for the row which matches more closer should be displayed.
A glimpse of my code
C++ Syntax (Toggle Plain Text)
for (int i=0; i<r; i++) { if( matrix_points[i][0] == ACE_Wide_To_Ascii(wEcuId.c_str()).char_rep()) { printf("\nEntered"); printf(matrix_points[i][1]); if(atoi(matrix_points[i][1]) == functionID) { printf("Entereted functionID\n"); if(matrix_points[i][2] == ACE_Wide_To_Ascii(testID.c_str()).char_rep()) // Comparing TestId in Excel file with the input TestId { printf("Entered testID\n"); if(atoi(matrix_points[i][3]) == testStat) // Comparing TestStatus in Excel file with the input TestStatus { printf("Entered testStatus\n"); if(atoi(matrix_points[i][4]) == ev.getNewState()) //Comparing ResponseType in Excel file with ErrorEvent's ResponseType { printf("Entered ResponseType\n"); if(matrix_points[i][5] == ev.getEvent()) //Comparing ResponseCode in Excel file with ErrorEvent's ResponseCode { printf("Entered ResponseCode\n"); qStrCT = matrix_points[i][6]; printf(qStrCT); ACE_WString wStrCT ((const ACE_WCHAR_T *) qStrCT.unicode(), qStrCT.length()); ACE_TString tStrCT = ACE_Wide_To_Ascii(wStrCT.c_str()).char_rep(); ct = wStrCT; //Retrieving Defaulttext qStrDT = matrix_points[i][7]; printf(qStrDT); ACE_WString wStrDT ((const ACE_WCHAR_T *) qStrDT.unicode(), qStrDT.length()); ACE_TString tStrDT = ACE_Wide_To_Ascii(wStrDT.c_str()).char_rep(); dt = wStrDT; //LOGI1(ACE_TEXT("Veena:matrix_points[i][7] change to ACE_WSTRING.[%s]"),ACE_Wide_To_Ascii(wStr.c_str()).char_rep()); } else { qStrCT = matrix_points[i][6]; printf(qStrCT); ACE_WString wStrCT ((const ACE_WCHAR_T *) qStrCT.unicode(), qStrCT.length()); ACE_TString tStrCT = ACE_Wide_To_Ascii(wStrCT.c_str()).char_rep(); ct = wStrCT; //Retrieving Defaulttext qStrDT = matrix_points[i][7]; printf(qStrDT); ACE_WString wStrDT ((const ACE_WCHAR_T *) qStrDT.unicode(), qStrDT.length()); ACE_TString tStrDT = ACE_Wide_To_Ascii(wStrDT.c_str()).char_rep(); dt = wStrDT; //LOGI1(ACE_TEXT("Veena:matrix_points[i][7] change to ACE_WSTRING.[%s]"),ACE_Wide_To_Ascii(wStr.c_str()).char_rep()); } } } } } } }
Thanks & Regards
Last edited by Dave Sinkula; Apr 19th, 2006 at 12:00 am.
![]() |
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
Views: 15242 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






