Hi guys, I am a beginner at this an was wondering if you could help. I have a text file which I can display in two columns as below, for example:
2.333,3433.333
233.33,33222.33
222.22,222.2222
I was wondering is it possible to store the two columns in two different arrays to go on and do further computations with? I am using the following code: -
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line; //this will contain the data read from the file
ifstream mecg ("SC1000.txt"); //opening the maternal ECG
if (mecg.is_open()) //if the file is open
{
while (! mecg.eof() ) //while the end of file is NOT reached
{
getline (mecg,line); //get one line from the file
cout << line << endl; //print to screen
}
mecg.close(); //closing the file
}
else cout << "Unable to open file"; //if file does not open
after fetching the line you can separate the line in two halfs and can store them in vector of string
you can do like this:
1 2 3 4 5 6 7 8 9 10 11 12
std::vector<std::string> col1, col2;
while (! mecg.eof() ) //while the end of file is NOT reached
{
getline (mecg,line); //get one line from the file
//take out first part
int pos = line.find(',');
col1.push_back(line.substr(0, pos));
//second part will be after the ','
col2.push_back(line.substr(pos + 1));
}
you have the full file now, print both the vectors, do the error checking in the code.