944,222 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14075
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 3rd, 2006
0

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

Expand 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? 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnnyrocket is offline Offline
5 posts
since Oct 2006
Oct 3rd, 2006
0

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

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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Oct 3rd, 2006
0

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

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.

C++ Syntax (Toggle Plain Text)
  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" 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;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnnyrocket is offline Offline
5 posts
since Oct 2006
Oct 3rd, 2006
0

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

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.

C++ Syntax (Toggle Plain Text)
  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" 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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 3rd, 2006
0

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

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?
Click to Expand / Collapse  Quote originally posted by WolfPack ...
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 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.
Moderator
Reputation Points: 3280
Solved Threads: 897
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006
Oct 4th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by WaltP ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnnyrocket is offline Offline
5 posts
since Oct 2006
Oct 4th, 2006
0

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

Well this being C++, I would go with something like
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 4th, 2006
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnnyrocket is offline Offline
5 posts
since Oct 2006
Oct 4th, 2006
0

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

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 4th, 2006
0

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

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
Moderator
Reputation Points: 3280
Solved Threads: 897
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: My Program Wont Work!! Help!!!!!
Next Thread in C++ Forum Timeline: How can I get a Program to Self Install itself in a given Directory?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC