Thread: read csv file

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    5

    read csv file

    Hi, I have a CSV file and I want to read the CSV file in .dat format. I have done the code and it managed to show data however, it only shows one data and the data is not the same as the csv file (perhaps is my varibale declare wrongly). How do I edit to read the file according to the csv file format?

    My CSV data (Stat.csv) data is as such

    Petrol Price Stat
    Time Cost (Per Litre) No. of Litre Total Cost
    10/10/2015 15:55 2.31 200 462
    11/10/2015 16:00 2.21 12 26.52
    12/10/2015 12:00 2.4 125 300
    12/10/2015 18:00 2.85 458 1305.3

    Below is my code
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    int main ()
    {
    
    ifstream inFile;
    ofstream outFile;
    
    string date;
    double cost;
    int no_of_litre;
    double total_cost;
    
    inFile.open("Stat.csv");
    outFile.open("Stat.dat");
    
    while(getline(inFile, date, ';') && getline(inFile, cost, ';') && getline(inFile, no_of_litre, ';') && getline(inFile, total_cost))
    {
    outFile << date << " " << cost << "" << no_of_litre << "" << total_cost << endl;
    }
    
    inFile.close();
    outFile.close();
    
    system ("PAUSE");
    return 0;
    }
    Last edited by Justin Chong; 09-27-2016 at 09:21 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your code is set up to look for ; between fields. If you can I would get new data that conforms to this.

    If you try to read these lines
    Code:
    Petrol Price Stat
    Time Cost (Per Litre) No. of Litre Total Cost
    There is no helpful delimiter between column headings.

    Contrast this with
    Code:
    Petrol Price Stat;
    Time;Cost (Per Litre);No. of Litre;Total Cost;
    Numbers and dates you can read with white space between information, but with strings it's ambiguous.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    5
    Sorry, I'm still quite new to C++.. So you mean I can remove ';' from the while loop?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Sorry, I'm still quite new to C++..
    I can sympathize, but I was talking more about the format of your file. I didn't mention C++ at all.
    So you mean I can remove ';' from the while loop?
    That's not what I said, but nevermind.

    In order to read the file as is, you will have to do something else with the first two lines. The formatting problem I mentioned before is only going to stop you from processing the dates and numbers.

    Among the mistakes in your attempt are how you used getline(). Getline is for strings only, so numbers like cost, number_of_litre, and total_cost, and the way you used them as part of the call to getline() will only make the program not compile.

    One option for example, I would ignore the first two lines and then extract the information directly:
    Code:
    string time;
    inFile.ignore(1000UL, '\n'); // or some large number
    inFile.ignore(1000UL, '\n'); // same number here
    while (inFile >> date >> time >> cost >> number_of_litre >> total_cost) {
       outFile<<' '<<date<<' '<<time<<' '<<cost<<' '<<number_of_litre<<' '<<total_cost<<endl;
    }
    Last edited by whiteflags; 09-27-2016 at 10:01 PM.

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    5
    Apologise, perhaps I misinterpret your reply wrongly.

    I have tried adding in the line of code you posted but the output file is empty.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Instead of using getline(), I recommend using the extraction operator>> instead since your file is space separated.

    You also need to check to insure that your files open correctly.

    Jim

  7. #7
    Registered User
    Join Date
    Sep 2016
    Posts
    5
    Hi, there are some changes to my code. However, currently I need some advice for the following:
    1) How to remove duplicate date from the printout
    2) How to enter a new line from the printout for the time.
    And the print out doesnt seems to be tally
    Below is my partial code for printing out the data
    Code:
    bool exit = false;
     while (exit==false) {
            cout << "Menu Option" << "\n" 
       << "1. Highest Price and Start Time" << "\n"
       << "2. Lowest Price and Start Time" << "\n"
       << "3. To End" << "\n" << "\n";
            cout << "Please input a value between 1 to 3: ";
      cin >> menuOption;
      cout << endl;
      switch (menuOption) {
      case 1:
      {
       int index = 0;
       double High = 0.0;
       
       for (int j = 0; j < salesVector.size(); j++) {
        if (salesVector[j].price > High) {
         index = j;
        }
       }
        cout << "Highest Price and Start Time" << endl;
        cout << "Date: " << salesVector[index].date << endl;
        cout << "Highest Price: $" << salesVector[index].price << endl;
        cout << "Starting Time: " << salesVector[index].time << "\n" << endl;
       exit = false;
       break;
      }
      case 2:
      {
       int index2 = 0;
       double Low = 0.0;
       
       for (int j = 0; j < salesVector.size(); j++) {
        if (salesVector[j].price < Low) {
         index2 = j;
        }
       }
       cout << "Lowest Price and Start Time" << endl;
       cout << "Date: " << salesVector[index2].date << endl;
       cout << "Lowest Price: $" << salesVector[index2].price << endl;
       cout << "Starting Time: " << salesVector[index2].time << "\n" << endl;
       exit = false;
       break;
      }
      case 3:
       exit = true;
       break;
      default:
       cout << "Error, Please Enter Again." << "\n" << endl;
       exit = false;
       break;
      }
     
     }
    }
    I am unable to upload any attachement, however, below is my file data
    Petrol Statistics
    Date/Time,Price ($),Volume,Value ($)
    10/10/2013 4:57,5.81,5000,29050
    10/10/2013 7:48,5.81,62728,364449.68
    10/10/2013 16:10,5.82,100,582
    10/10/2013 17:10,5.86,150,879
    10/10/2013 17:11,5.23,169,883.87
    10/10/2013 17:14,5.81,451,2620.31
    10/10/2013 18:10,5.81,5000,29050

    Below is my output results
    Highest Price and Start Time
    Date: 10/10/2013 10/10/2013
    Highest Price: $5.82
    STarting Time: 4.57:48 18:10:48
    Lowest Price and Start Time
    Date: 10/10/2013
    Highest Price: $5.82
    STarting Time: 4.57:48

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    And the print out doesnt seems to be tally
    Have you checked to insure you're reading the file correctly?

    You also need to use a consistent indent style.


    Jim

  9. #9
    Registered User
    Join Date
    Sep 2016
    Posts
    5
    Hi Jim

    There are some changes to my CSV data

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There are some changes to my CSV data
    So how does this answer my question?

    The code you posted in your last snippet doesn't show how you're trying to read the data so I have no idea if you're even reading the file correctly.

    Also posted here.

    Jim
    Last edited by jimblumberg; 10-11-2016 at 09:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2014, 07:01 AM
  2. Replies: 7
    Last Post: 12-07-2012, 10:44 PM
  3. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  4. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM

Tags for this Thread