Read from CSV file in C++

Hi everyone,
I am beginner to C++ and I have encountered a little problem. I have saved 5 numbers in a CSV file. Now I want to read these numbers from the file and then store each number in a single array. How do I do it?

I know how to read from a file but everytime I read numbers from file and display it on screen it also displays the commas after every number. How do I remove comma? I just want to display numbers store in that CSV file and then store these number in a single array.

Any kind of help will be appreciated!
How do you read the numbers?
If your file looks loke this
1,2,3,4,5
you can read the numbers into a vector like this:
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
#include <iostream> 
#include <fstream>
#include <string>
#include <vector>

typedef std::vector<std::string> StringVector;
typedef std::vector<int> IntVector;

void ReadNumbers(const std::string& filename, char sep, IntVector& output);

int main ()
{
  std::string filename = "C:\\Temp\\numbers.txt";
  IntVector numbers;

  ReadNumbers(filename, ',', numbers);

  // Output the numbers
  std::cout << "Numbers read:\n";
  for (int i=0; i < numbers.size(); i++)
    std::cout << numbers[i] << '\t';
  std::cout << "\n\n";

  system("pause");
}

void ReadNumbers(const std::string& filename, char sep, IntVector& output)
{
  std::ifstream src(filename);

  if (!src)
  {
    std::cerr << "\aError opening file.\n\n";
    exit(EXIT_FAILURE);
  }
  std::string buffer;
  size_t strpos = 0;
  size_t endpos;
  while(std::getline(src, buffer))
  {
    endpos= buffer.find(sep); 
    while (endpos < buffer.length())
    {  
      output.push_back(std::stoi(buffer.substr(strpos,endpos - strpos)));
      strpos = endpos + 1;
      endpos = buffer.find(sep, strpos);
    }
    output.push_back(std::stoi(buffer.substr(strpos)));
  }
}


If you use floats you have to use std::stof instead of std::stoi
@Thomas1065 That seems like a very complicated and error prone way to read a few numbers from a file, why not just use the stream extraction operators instead?
@keskiverto: Yes, I want to read the numbers.

@Thomas1965: Hey,thanks for your help. I really appreciate it. I haven't learnt vectors yet. Is there any other way to read numbers without using vector?
Please post the code showing what you've tried. Reading from a file is basically the same as reading from the console.
If all the values are on a single line, you could read them like this.
data.txt:
1.5,2.718,3.14,4,5.6


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    std::ifstream fin("data.txt");
    std::vector<double> numbers;
    
    char ch;
    double num;
    
    while (fin >> num)           // read a number
    {
        numbers.push_back(num);  // store the number 
        fin >> ch;               // read and discard the comma
    }
    
    // display the result
    for (unsigned int i=0; i<numbers.size(); i++)
        std::cout << numbers[i] << '\n';

}

Output:
1.5
2.718
3.14
4
5.6


@jlb
That seems like a very complicated and error prone way to read a few numbers from a file

I am sure there must be easier ways.
I wasn't aware that stoi might throw an exception - I am still a beginner with C++.
why not just use the stream extraction operators instead?

I didn't know how to skip the comma and didn't have the time to do proper research.
How would you do it?

I didn't know how to skip the comma and didn't have the time to do proper research.
How would you do it?

See the post by Chervil above for perhaps the simplest way.

I wasn't aware that stoi might throw an exception

Yes, and that's a good thing, because it doesn't just silently fail.

With your code what happens if the input file ends with the delimiter (the comma in this case)?
Topic archived. No new replies allowed.