Reading in certain columns from CSV files into array C++

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

Join Date: Oct 2006
Posts: 5
Reputation: johnnyrocket is an unknown quantity at this point 
Solved Threads: 0
johnnyrocket johnnyrocket is offline Offline
Newbie Poster

Reading in certain columns from CSV files into array C++

 
0
  #1
Oct 3rd, 2006
I've placed a CSV file into an array with C++ with each row being a single array element, but the file contains many, many columns that I don't need. I only need columns 1,2,3,4,6,7 out of about 50. Is is best to read the full value into the array and then edit it or only read in those values that I need? Either way, I'm stuck and don't know how to proceed either way. I'm new at this and I've been unable to find resources about this online. Any help would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Reading in certain columns from CSV files into array C++

 
0
  #2
Oct 3rd, 2006
Originally Posted by johnnyrocket View Post
I've placed a CSV file into an array with C++ with each row being a single array element, but the file contains many, many columns that I don't need. I only need columns 1,2,3,4,6,7 out of about 50. Is is best to read the full value into the array and then edit it or only read in those values that I need?
This depends on what you are going to do with the data read. If you are going to edit the data in those columns and write it back to a file, along with the rest of the columns, you will have to read and store all the data. If you only want the values, and they are discarded after use, you can create an array of only 6 elements with multiple rows, and store the columns you need to that array.

As for how to proceed, paste the current code where you are reading the csv values into the array, and maybe we can work something out.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 5
Reputation: johnnyrocket is an unknown quantity at this point 
Solved Threads: 0
johnnyrocket johnnyrocket is offline Offline
Newbie Poster

Re: Reading in certain columns from CSV files into array C++

 
0
  #3
Oct 3rd, 2006
Remember I'm new at this, so I'm sure my code is horrible. This is in Visual Studio 2005. The Console::WriteLine and the cout of the lineCount are only for my purposes to make sure the program was actually working. They will be removed later.

  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. using namespace System;
  7. using namesapce System::IO;
  8. using namespace System::Collections;
  9.  
  10. int _tmain()
  11. {
  12. StreamReader^ reader;
  13. int lineCount = 0;
  14. reader = gcnew StreamReader("<a rel="nofollow" class="t" href="http://www.daniweb.com/techtalkforums/" target="_blank">\\\\<server>\\<file>.csv</a>");
  15. String^ data;
  16.  
  17. ArrayList^ theArray = gcnew ArrayList(lineCount);
  18.  
  19. while(0<reader->Peek())
  20. {
  21. data = reader->ReadLine();
  22. theArray->Add(data);
  23. Console::WriteLine(theArray[lineCount]);
  24. lineCount++;
  25. }
  26.  
  27. cout << lineCount << endl;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reading in certain columns from CSV files into array C++

 
0
  #4
Oct 3rd, 2006
Originally Posted by johnnyrocket View Post
Remember I'm new at this, so I'm sure my code is horrible. This is in Visual Studio 2005. The Console::WriteLine and the cout of the lineCount are only for my purposes to make sure the program was actually working. They will be removed later.

  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. using namespace System;
  7. using namesapce System::IO;
  8. using namespace System::Collections;
  9.  
  10. int _tmain()
  11. {
  12. StreamReader^ reader;
  13. int lineCount = 0;
  14. reader = gcnew StreamReader("<a rel="nofollow" class="t" href="http://www.daniweb.com/techtalkforums/" target="_blank">\\\\<server>\\<file>.csv</a>");
  15. String^ data;
  16.  
  17. ArrayList^ theArray = gcnew ArrayList(lineCount);
  18.  
  19. while(0<reader->Peek())
  20. {
  21. data = reader->ReadLine();
  22. theArray->Add(data);
  23. Console::WriteLine(theArray[lineCount]);
  24. lineCount++;
  25. }
  26.  
  27. cout << lineCount << endl;
That ain't c++ sorrie.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading in certain columns from CSV files into array C++

 
0
  #5
Oct 3rd, 2006
Originally Posted by johnnyrocket View Post
I've placed a CSV file into an array with C++ with each row being a single array element, but the file contains many, many columns that I don't need. I only need columns 1,2,3,4,6,7 out of about 50. Is is best to read the full value into the array and then edit it or only read in those values that I need?
Originally Posted by WolfPack View Post
This depends on what you are going to do with the data read. If you are going to edit the data in those columns and write it back to a file, along with the rest of the columns, you will have to read and store all the data. If you only want the values, and they are discarded after use, you can create an array of only 6 elements with multiple rows, and store the columns you need to that array.
Originally Posted by johnnyrocket View Post
Remember I'm new at this, so I'm sure my code is horrible. This is in Visual Studio 2005. The Console::WriteLine and the cout of the lineCount are only for my purposes to make sure the program was actually working. They will be removed later.
This response doesn't seem to answer any of WolfPack's questions. We still don't know enough to answer your question. And yes, your format is horrible. After every { indent 4 spaces. Before every } unindent. Then you program can be followed.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 5
Reputation: johnnyrocket is an unknown quantity at this point 
Solved Threads: 0
johnnyrocket johnnyrocket is offline Offline
Newbie Poster

Re: Reading in certain columns from CSV files into array C++

 
0
  #6
Oct 4th, 2006
Originally Posted by WaltP View Post
This response doesn't seem to answer any of WolfPack's questions. We still don't know enough to answer your question. And yes, your format is horrible. After every { indent 4 spaces. Before every } unindent. Then you program can be followed.
I need to read in the data, sort it, add a few columns from another CSV file and output it to a file. So what I need is to read in a CSV file of about 500 rows and 50 columns, keeping all the rows, but only keeping columns 1,2,3,4,6,7 and appending those elements with data from another CSV file to go into an output report. So I'll end up with about 500 rows and 10 columns. Again, I'm new at this and struggling at it. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
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 in certain columns from CSV files into array C++

 
0
  #7
Oct 4th, 2006
Well this being C++, I would go with something like
  1. class csvRow {
  2. public:
  3. parseLine(std::string);
  4. std::string operator[](int) const;
  5. private:
  6. std::vector< std::string > row;
  7. };
Where parseLine takes a line and splits out all the comma separated values, and stores them in the internal vector.
The overload of the [] array operator allows you to pick out individual fields.

So you could say for example something like this
  1. csvRow foo;
  2. foo.parseLine("1,2,3,\"hello\",5,6");
  3. cout << foo[0] << foo[2] << endl;
The whole file would just be a std::vector of csvRow's
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 5
Reputation: johnnyrocket is an unknown quantity at this point 
Solved Threads: 0
johnnyrocket johnnyrocket is offline Offline
Newbie Poster

Re: Reading in certain columns from CSV files into array C++

 
0
  #8
Oct 4th, 2006
I'm not familiar with vectors. So I need a csvRow for each element in the array? I'm having trouble creating those. What else do I need to add to the code?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
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 in certain columns from CSV files into array C++

 
0
  #9
Oct 4th, 2006
Ok, start with the basics - can you split this line at each comma?
"1,2,3,\"hello, world\",5,6"

Start real simple, just print each field as you find it, say
Found 1
Found 2
Found 3
Found hello, world
Found 5
Found 6

Test it with a variety of lines to make sure that bit of the code works.

This is the essence of the problem, without which all the packaging will do you no good at all. My csvRow thing just wraps it up into a useful class where you can treat each field of the CSV as a separate index of an array.

> I'm not familiar with vectors
Consider it an opportunity to learn about them then - read a book, find a tutorial or two.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading in certain columns from CSV files into array C++

 
0
  #10
Oct 4th, 2006
Since you keep explaining you are new (thinking we'll forget I guess), I'll assume you know practically nothing about C++ but know just enough to understand arrays and the string type.
  • Define a two dimensional array of strings with as many rows and colums as you need
  • Open the data file
  • Start a loop to read each line into another string
    • read a line
    • look for a comma
    • move the previous substring into your string array into col 1
    • look for the next comma
    • move the previous substring into your string array into col 2
    • keep going until you have all the columns saved
    • loop back
  • Close the file
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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