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.
@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?
@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?
#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 (unsignedint i=0; i<numbers.size(); i++)
std::cout << numbers[i] << '\n';
}