Reading of a *.CSV File

This is a discussion on Reading of a *.CSV File within the C Programming forums, part of the General Programming Boards category; Quick question for all of you great experts out there...I have a file called Currency Conversion Rates.csv that I was ...

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

    Reading of a *.CSV File

    Quick question for all of you great experts out there...I have a file called Currency Conversion Rates.csv that I was wondering if there is a way to read the entire file into an array, 24x24 ([24] [24] for doing some mathematical operations. Should I include a copy of the file here so that you can see what the data looks like? (The data can all be defined as float).

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The fgets() function reads a line of data from a file into a buffer. The sscanf() function extracts values from a buffer and puts them in variables based on a format string.

    So if we have a file looking like this:
    Code:
    1.345, 34.527, 54.9
    56.988, 0.7, 397.0
    we might write code like this:
    Code:
    while (fgets(...)) /* Read a line of text from the file. */
    { 
       /* Extract the float values and put them in an array. */
       sscanf(buf, "%f, %f, %f", &floatarr[0], &floatarr[1], &floatarr[2]); 
    }
    If you want further help, could you provide a sample of the data and post the code that you have written and details on why it is not working?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Reading a *.csv file into an array
    By AQWst in forum C++ Programming
    Replies: 5
    Last Post: 12-18-2004, 01:12 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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