Reading CSV in VC++

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 15,662
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: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading CSV in VC++

 
0
  #11
Apr 13th, 2006
>> 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
  1. char *matrix_points[255][255]; // 2d array of pointers
  2.  
  3. matrix_points[r][c] = new char[strlen(line)+1];
  4. strcpy(matrix_points[r][c], line);

  1. while(input_file.getline(line, sizeof(line), '\n')) // to read lines
  2. {
  3. 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.
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
  #12
Apr 13th, 2006
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++;
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
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: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading CSV in VC++

 
0
  #13
Apr 13th, 2006
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
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
  #14
Apr 16th, 2006
Hi Dragon,
Thanks for the reply. It works. Now the problem comes when I assign the matrix_points, which is of type QString to a variable in my application which is of ACE_WString Type, it gives a problem. What kind of typecasting method can we apply to overcome it?

Thanks & Regards,
Narender
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
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: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading CSV in VC++

 
0
  #15
Apr 16th, 2006
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.
  1. QString str = "Hello World";
  2. ACE_WString wstring(str.??());
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
  #16
Apr 17th, 2006
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

  1. 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)


  1. int CCommonErrorManager::requestErrorHandling(ErrorEvent& ev, ACE_WString& codeText, ACE_WString& defaultText, int& functionID,ACE_WString& testID, int& testStat)
  2. {
  3.  
  4. QString matrix_points[6][8];
  5. //std::ifstream input_file("C:/NDA/NissanNewApplication/Dev/CommonErrorManager/Src/ErrorMessageList.csv");
  6. std::ifstream input_file("../../Src/ErrorMessageList.csv");
  7.  
  8. char line[255];
  9. input_file.clear();
  10. input_file.seekg(0);
  11.  
  12. int r = 0, c = 0, colcnt = 0;
  13. QString linecol, field;
  14.  
  15. while(input_file.getline(line, sizeof(line), '\n')) // to read lines
  16. {
  17. linecol = line;
  18. while(linecol != "")
  19. {
  20. //linecol = linecol.stripWhiteSpace();
  21. int pos = linecol.find(',');
  22. if(pos > 0)
  23. {
  24.  
  25. //linecol = linecol.left(pos); // substr function does not work in VC++.
  26. matrix_points[r][c] = linecol.mid(0,pos);
  27. //printf(matrix_points[r][c]);
  28. //printf(" ");
  29. linecol = linecol.mid(pos+1);
  30. c++;
  31. }
  32. else
  33. {
  34. matrix_points[r][c] = linecol;
  35. //printf(matrix_points[r][c]);
  36. linecol = "";
  37. colcnt = c;
  38. c = 0;
  39. r++;
  40. printf("\n");
  41. }
  42. }
  43. }
  44.  
  45. //Retrieving Value
  46. ACE_WString wStr = codeText;
  47. ACE_TString tStr = ACE_Wide_To_Ascii(wStr.c_str()).char_rep();
  48.  
  49. for (int i=0; i<r; i++)
  50. if(matrix_points[i][0] == "ECU_MODEL_KWP_ABS")
  51. {
  52. //printf(matrix_points[i][3]+" " );
  53. //printf(matrix_points[i][6]+" " );
  54. //printf(matrix_points[i][7] + "\n");
  55.  
  56. <div class="bbquote"> <div class="tlc"><div class="tli">&bull;</div></div> <div class="blc"><div class="bli">&bull;</div></div> <div class="trc"><div class="tri">&bull;</div></div> <div class="brc"><div class="bri">&bull;</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);
  57.  
  58. }
  59.  
  60.  
  61. //Initializing the output parameters codeText & defaultText. Both of these need to be searched in the database.
  62. //codeText = L"";
  63. defaultText = L"";
  64.  
  65. /*
  66. Acquiring Response Code
  67. ev.getEvent() -> Response code such as 21h,23h
  68. */
  69. int rcode;
  70. rcode = ev.getEvent();
  71.  
  72.  
  73.  
  74. /* ev.getType -> Error Type */
  75.  
  76. if(ev.getType() == ErrorEvent::Unknown)
  77. {
  78. LOGI(ACE_TEXT("Error Type:[Unknown]\n"));
  79. /* Channel_Error_Notification -> No Response */
  80. if(ev.getNewState() == ICommManager::CHANNEL_ERROR_NOTIFICATION)
  81. {
  82. LOGI(ACE_TEXT("Channel Error Notification....[No Response]"));
  83. m_pLCP->reLink();
  84. //m_pLCP->ReTry();
  85. return 2;
  86. }
  87. else if((ev.getNewState() == ICommManager::NACK_COMMAND) || (ev.getNewState() == ICommManager::NACK_EPI))
  88. {
  89. switch(rcode) {
  90.  
  91. LOGI(ACE_TEXT("Determining Response Code"));
  92.  
  93. /* Negative Response 21h and 23h */
  94. case 0x21 :
  95. case 0x23 :
  96. LOGI1(ACE_TEXT("The Response Code is [%x]"),rcode);
  97. //ev.getSource() -> For ECUID.
  98. if(ev.getSource() == "ECU_MODEL_CAN_SMKEY") //tStr<-->"ECU_MODEL_CAN_SMKEY"
  99. {
  100. LOGI(ACE_TEXT("Checking ECU Id....ECU_MODEL_CAN_SMKEY"));
  101. LOGI(ACE_TEXT("[ReLink] and [ReTry()] are called"));
  102. m_pLCP->reLink();
  103. }
  104. else
  105. {
  106. LOGI(ACE_TEXT("Only [ReTry()] is called"));
  107. m_pLCP->retry();
  108. }
  109. return 2;
  110. break;
  111. /* Negative Response 78h */
  112. case 0x78:
  113. LOGI(ACE_TEXT("Negative Response Code : [78h]"));
  114. return 1;
  115. break;
  116. default:
  117. LOGI(ACE_TEXT("Response Code other than 21h,23h,78h"));
  118. if(ev.getSource() == "ECU_MODEL_CAN_SMKEY" && counter == 0) //tStr<-->"ECU_MODEL_CAN_SMKEY"
  119. {
  120. LOGI(ACE_TEXT("ECU Id: [ECU_MODEL_CAN_SMKEY]"));
  121. counter = counter + 1;
  122. m_pLCP->reLink();
  123. return 2;
  124. }
  125. else
  126. {
  127. codeText = L"Default";
  128. defaultText = L"Default";
  129. LOGI(ACE_TEXT("Displaying Error in GUI"));
  130. /* set the counter to 0 */
  131. counter = 0;
  132. /* Callback Function */
  133. //m_pCB->showErrorScreen();
  134.  
  135. return 3;
  136. }
  137. break;
  138. } // end of switch
  139. } // end of else if of getNewState()=CHANNEL_ERROR_NOTIFICATION
  140. else // else of getNewState()=NACK_COMMAND OR NACK_EPI
  141. {
  142. codeText = L"Default";
  143. defaultText = L"Default";
  144. LOGI(ACE_TEXT("Unknown Error is encountered."));
  145. return 3;
  146. }
  147.  
  148. } // if getType()
  149.  
  150. else // Else of getType()
  151. {
  152. codeText = L"Default";
  153. defaultText = L"Default";
  154. LOGI(ACE_TEXT("A problem has occured at hardware level"));
  155. return 3;
  156. }
  157. //return 3;
  158.  
  159. } // end of Function
Last edited by Dave Sinkula; Apr 18th, 2006 at 11:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
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: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading CSV in VC++

 
0
  #17
Apr 17th, 2006
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.

  1. QString str = "Hello";
  2. ACE_WString w(str.ascii());

I'm just sort of guessing about the above because I don't have either of those classes.
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
  #18
Apr 18th, 2006
Hi Dragon,
Thank you. Actually I am able to manipulate the values from the csv file based on the row,column concept. I have now need to manipulate the values based on the header names. Concept being same as last, I would like to query based on column names.

Thanks & Regards,
Narender
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Reading CSV in VC++

 
0
  #19
Apr 18th, 2006
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.
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
  #20
Apr 18th, 2006
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
  1. for (int i=0; i<r; i++)
  2. {
  3. if( matrix_points[i][0] == ACE_Wide_To_Ascii(wEcuId.c_str()).char_rep())
  4. {
  5. printf("\nEntered");
  6. printf(matrix_points[i][1]);
  7. if(atoi(matrix_points[i][1]) == functionID)
  8. {
  9. printf("Entereted functionID\n");
  10. if(matrix_points[i][2] == ACE_Wide_To_Ascii(testID.c_str()).char_rep()) // Comparing TestId in Excel file with the input TestId
  11. {
  12. printf("Entered testID\n");
  13. if(atoi(matrix_points[i][3]) == testStat) // Comparing TestStatus in Excel file with the input TestStatus
  14. {
  15. printf("Entered testStatus\n");
  16. if(atoi(matrix_points[i][4]) == ev.getNewState()) //Comparing ResponseType in Excel file with ErrorEvent's ResponseType
  17. {
  18. printf("Entered ResponseType\n");
  19. if(matrix_points[i][5] == ev.getEvent()) //Comparing ResponseCode in Excel file with ErrorEvent's ResponseCode
  20. {
  21. printf("Entered ResponseCode\n");
  22. qStrCT = matrix_points[i][6];
  23. printf(qStrCT);
  24. ACE_WString wStrCT ((const ACE_WCHAR_T *) qStrCT.unicode(), qStrCT.length());
  25. ACE_TString tStrCT = ACE_Wide_To_Ascii(wStrCT.c_str()).char_rep();
  26. ct = wStrCT;
  27.  
  28. //Retrieving Defaulttext
  29. qStrDT = matrix_points[i][7];
  30. printf(qStrDT);
  31. ACE_WString wStrDT ((const ACE_WCHAR_T *) qStrDT.unicode(), qStrDT.length());
  32. ACE_TString tStrDT = ACE_Wide_To_Ascii(wStrDT.c_str()).char_rep();
  33. dt = wStrDT;
  34. //LOGI1(ACE_TEXT("Veena:matrix_points[i][7] change to ACE_WSTRING.[%s]"),ACE_Wide_To_Ascii(wStr.c_str()).char_rep());
  35. }
  36. else
  37. {
  38. qStrCT = matrix_points[i][6];
  39. printf(qStrCT);
  40. ACE_WString wStrCT ((const ACE_WCHAR_T *) qStrCT.unicode(), qStrCT.length());
  41. ACE_TString tStrCT = ACE_Wide_To_Ascii(wStrCT.c_str()).char_rep();
  42. ct = wStrCT;
  43.  
  44. //Retrieving Defaulttext
  45. qStrDT = matrix_points[i][7];
  46. printf(qStrDT);
  47. ACE_WString wStrDT ((const ACE_WCHAR_T *) qStrDT.unicode(), qStrDT.length());
  48. ACE_TString tStrDT = ACE_Wide_To_Ascii(wStrDT.c_str()).char_rep();
  49. dt = wStrDT;
  50. //LOGI1(ACE_TEXT("Veena:matrix_points[i][7] change to ACE_WSTRING.[%s]"),ACE_Wide_To_Ascii(wStr.c_str()).char_rep());
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }


Thanks & Regards
Last edited by Dave Sinkula; Apr 19th, 2006 at 12:00 am.
Reply With Quote Quick reply to this message  
Reply

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




Views: 15242 | Replies: 19
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC