mercredi 31 décembre 2014

read data from file and manipulate


#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;

int main(){

//Declare file streams & open the file
ifstream input;
input.open("price.txt");

if(input.fail())
{
cout << "price.txt could not be accessed!" << endl;
exit(1);
}

string temp, test; //String data from file, to be converted to an double

//Get data from file
for(int i = 1; i <= 10; i++)
{
if(!input.eof()){
getline(input,temp); //Get 1 line from the file and place into temp
cout<< i << ". " << temp <<"\n";
test[i] = atof(temp.c_str()); //set the test variable to the double data type of the line

}
}

input.close();
return 0;
}


The question is how can I use the data I get from the file to assign to another variable so I can use the variable guy to the whole program of c++


my txt file looks like 3.00 4.00 5.00 3.20 3.00 3.55 1.60 4.90 2.00 1.50


Aucun commentaire:

Enregistrer un commentaire