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.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
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.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
using namespace System;
using namesapce System::IO;
using namespace System::Collections;
int _tmain()
{
StreamReader^ reader;
int lineCount = 0;
reader = gcnew StreamReader(" <a href="http://www.daniweb.com/techtalkforums/">\\\\<server>\\<file>.csv</a> ");
String^ data;
ArrayList^ theArray = gcnew ArrayList(lineCount);
while(0<reader->Peek())
{
data = reader->ReadLine();
theArray->Add(data);
Console::WriteLine(theArray[lineCount]);
lineCount++;
}
cout << lineCount << endl;
That ain't c++ sorrie.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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.
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 ofWolfPack'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.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Well this being C++, I would go with something like
class csvRow {
public:
parseLine(std::string);
std::string operator[](int) const;
private:
std::vector< std::string > row;
};
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
csvRow foo;
foo.parseLine("1,2,3,\"hello\",5,6");
cout << foo[0] << foo[2] << endl;
The whole file would just be a std::vector of csvRow's
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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 stringread 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
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Ok. So it looks like I was approaching the problem incorrectly. If I create the 2-dimensional array, I think I can remove entire columns using a loop with ArrayList->RemoveAt. For example, to remove column 5: theArray->RemoveAt[i][5] and iterate through the end of the rows. I'll give it a shot. Thanks.
Not what I said. Please read againcarefully to see the easy solution. You don't start with 50 columns. You start with the total you need at the end of processing (like 10).
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Ok. So it looks like I was approaching the problem incorrectly. If I create the 2-dimensional array, I think I can remove entire columns using a loop with ArrayList->RemoveAt. For example, to remove column 5: theArray->RemoveAt[i][5] and iterate through the end of the rows. I'll give it a shot. Thanks.
Nope, there ain't no ArrayList->RemoveAt in c++.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439