Hi,
I am trying to extract data from a text file into an array or even a vector. The example data in the file "data.txt" is in the following manner
Home number = 123456
Mobile number = 789012
Office number = 567987
Balance = 46.56
...
...
...
I want to extract the values after the '=' sign and store them in an array.
i tired using the getline and ignore functions to extract the data, but it doesnt seem to be working fine.
the code i wrote is
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
char *arr;
vector<double> v(4);
int length;
int i = 0;
ifstream input("data.txt", ios::in);
// Obtaining the length of file
input.seekg (0, ios::end);
length = input.tellg();
input.seekg (0, ios::beg);
arr = new char [length];
if(input)
{
input.ignore('=');
input.getline(arr,length);
v[i] = atof(arr);
i++;
}
for(i = 0; i < 4; i++)
cout << v[i] << endl;
}
Thanks