Reading a *.csv file into an array

This is a discussion on Reading a *.csv file into an array within the C++ Programming forums, part of the General Programming Boards category; I would like to read in a *.csv file into an array defined as 24 x 24, but not sure ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    41

    Reading a *.csv file into an array

    I would like to read in a *.csv file into an array defined as 24 x 24, but not sure how to do it. The name of the file is Currency Exchange Rates.csv. Can I use the file using this name, or should I change its name before working with it?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You could always read the answer I posted the last time you asked this question.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    41
    I looked at this answer and am still a bit confused. I am including 48 records below as sample data.


    1.0000000,0.7785980,0.3613370,1.8514000,0.8285000, 0.1208240,0.1747490,1.2982000,0.1285890,0.0222220, 0.0095940,0.2631580,0.0881680,0.7086020,0.1596320, 0.6046680,0.1658810,0.0009350,0.0095350,0.1447010, 0.8553590,0.0306420,0.0249000,0.0005220
    1.2843600,1.0000000,0.4640870,2.3778600,1.0640900, 0.1551820,0.2244400,1.6673600,0.1651550,0.0285410, 0.0123220,0.3379890,0.1132390,0.9101000,0.2050250, 0.7766110,0.2130520,0.0012010,0.0122460,0.1858480, 1.0985900,0.0393550,0.0319810,0.0006710

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If it's just a long list of floating-point values separated by a comma, you really don't need to do more than this:
    Code:
    for (int i = 0; i < 24; i++) {
      for (int j = 0; j < 24; j++) {
        in>> array[i][j];
        // You can add error checking for the expected
        // comma here if you really want to.
        in.ignore();
      }
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The name of the file is Currency Exchange Rates.csv. Can I use the file using this name
    Sure, just put this in front of Prelude's code to open the file for reading:
    Code:
    ifstream in( "Currency Exchange Rates.csv" );

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I assume the values have a particular order to them, you may want to confirm that order. Otherwise all you have is a whole lot of floats.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 03:17 PM
  3. Replies: 5
    Last Post: 10-02-2005, 01:15 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21