Taking inputs from csv file

Hello I have a csv file which consists of numbers separated by commas, like the following:

0.6667,0.6668,0.6669........
0.7773,0.7774,0.7775.......
.
.
.
.

I need these values for my calculations in the code. How do I get these values from the file and put them in an 2D array?

Your help is appreciated.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

using vec    = vector<double>;
using matrix = vector<vec   >;


//==========================================================


matrix readCSV( string filename )
{
   matrix M;

   ifstream in( filename );
   string line;
   while ( getline( in, line ) )                   // read a whole line of the file
   {
      stringstream ss( line );                     // put it in a stringstream (internal stream)
      vec row;
      string data;
      while ( getline( ss, data, ',' ) )           // read (string) items up to a comma
      {
         row.push_back( stod( data ) );            // use stod() to convert to double; put in row vector
      }
      if ( row.size() > 0 ) M.push_back( row );    // add non-empty rows to matrix
   }
   return M;
}


//==========================================================


void write( const matrix &M )
{
   const int w = 12;
// cout << fixed;
   for ( auto row : M )
   {
      for ( auto e : row ) cout << setw( w ) << e << ' ';
      cout << '\n';
   }
}


//==========================================================


int main()
{
   matrix M = readCSV( "data.csv" );
   write( M );
}


//========================================================== 


data.csv:
0.6667,0.6668,0.6669
0.7773,0.7774,0.7775


      0.6667       0.6668       0.6669 
      0.7773       0.7774       0.7775
thank you so much :)
in my original csv data file, it has got many rows and columns. Could you please help me with a code where I can count the number of rows and columns and then put them in a 2D array?

Thanks,
The code already puts them in a 2D array (a vector<vector<double>>).

If you need to know the number of rows and columns then just change main() to
1
2
3
4
5
6
7
int main()
{
   matrix M = readCSV( "data.csv" );
   cout << "Number of rows = " << M.size() << '\n';
   cout << "Number of columns = " << M[0].size() << '\n';
   write( M );
}
Last edited on
Thanks again,
But when I am trying to compile it, my compiler is throwing the following errors:

error: expected nested-name-specifier before ‘vec’
using vec = vector<double>;
^
csv_test2.cc:10:8: error: expected nested-name-specifier before ‘matrix’
using matrix=vector<vec>;
^
csv_test2.cc:12:15: error: expected initializer before ‘readCSV’
double matrix readCSV(string filename)



Your help is greatly appreciated.
using vec = vector<double>;This is C++11, maybe your compiler is too old or C++11 support is not enabled. What do you use ?
An alternative:
1
2
typedef vector<double> vec;
typedef vector<vec> matrix;

I am using code::blocks as an IDE. It uses GNU GCC compiler

after making the changes, it now shows an error

error: no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(std::string&)'|
Last edited on
Please upgrade your compiler - or change one of the compile options to -std=c++11. (Read your IDE's documentation.)

If it baulks at that then it will also throw a paddy at range-based for loops, use of auto, ....
Hi,

Could there be an alternative way of writing the code? I upgraded the codeblock compiler to follow C++11, but it still throws an error stating that "stod was not declared in this scope".

Thank you once again.
I use Code::Blocks 17.12 and lastchance's code compiles.
Thanks @Thomas1965, I had no way of checking that in an IDE.

@Saptarnee,
As a short-term fix you can create your own function stod()
1
2
3
4
5
6
double stod( string s )
{
   double result;
   stringstream( s ) >> result;
   return result;
}


Put it somewhere above the point of first use.

If it now works then I would recommend renaming it (and the call to it) so that it doesn't conflict with the standard library version when you do eventually get a fully up-to-date compiler.
Firstly thank you so much @lastchance and @Thomas1965. My code compiled after creating a stod() function.

But the thing that concerns me is, I downloaded CodeBlocks version 17.12. Still it throws an error for the stod(). It says stod() is not declared withing this scope.

Is there any way to fix this? I thought C++11 would easily compile in the 17.12 version of CodeBlocks.

Can you upload the whole project somewhere, maybe some settings are missing.
Is there any way to fix this? I thought C++11 would easily compile in the 17.12 version of CodeBlocks.

If you have properly set the project setting to use C++11 or higher the problems could be the version of your compiler. If you're using the g++ compiler you need at least version 4.8.1 for full C++11 support (which includes the stoX() functions). I suggest you use an even more modern compiler if possible and using one of the more current standards (C++14 version 6.1, or C++17 version 7.2).

CodeBlocks is an IDE. IDE is not a compiler. IDE does use a compiler. IDE install might include a compiler. The compiler can be replaced.

GCC is a compiler. For many versions, it did use legacy C++ by default. Newer C++ standard support had to be enabled with a command line option. An IDE user does not run compiler from command line. IDE user sets IDE options that the IDE passes to the compiler.
Newer C++ standard support had to be enabled with a command line option.

Actually strict C++ standard support still has to be enabled with a command line option. By default g++ uses -std=gnuXXX which allows several hacks to be used that should be avoided while learning the language, IMO.


Topic archived. No new replies allowed.