#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <sstream>
// extract values in columns 3, 4 and 5 of the line into an array of three double
// return true if extraction is successful
bool extract( std::string line, std::array< double, 3 >& data )
{
// create a string stream to read from the line
std::istringstream stm(line) ;
int col1, col2 ; // not_wanted, read and discard
returnbool( stm >> col1 >> col2 >> data[0] >> data[1] >> data[2] ) ;
}
// return a vector containing data read from the input stream
std::vector< std::array< double, 3 > > get_data( std::istream& stm )
{
std::vector< std::array< double, 3 > > result ;
std::string line ;
while( std::getline( stm, line ) ) // for each line in the input stream
{
std::array< double, 3 > data ;
// add the data to result if extraction is successful
if( extract( line, data ) ) result.push_back(data) ;
// else { /* badly formed line in the input stream */ }
}
return result ;
}
int main() // simple test driver
{
// simulate an input file containing test data (in actual code, use std::ifstream instead)
std::istringstream file( "1 1 137.254791 36.164043 1.663292 271.845428 0.003446 94178.367188 3.831074 62.038136 0.000000 0.000000 307.386322\n""2 1 137.255768 36.164043 1.651848 271.855011 0.003446 94184.335938 3.819067 61.833946 0.000000 0.000000 307.287109\n""3 1 137.256744 36.164043 1.640299 271.861389 0.003446 94184.398438 3.807216 61.646248 0.000000 0.000000 307.271057\n""4 1 137.257736 36.164043 1.628936 271.861908 0.003446 94173.765625 3.796037 61.468674 0.000000 0.000000 307.290863\n""5 1 137.258713 36.164043 1.886245 271.794739 0.003462 94096.921875 4.074421 65.879463 0.000000 0.000000 308.017212\n""6 1 137.259689 36.164043 1.911562 271.773621 0.003464 94057.750000 4.105538 66.371078 0.000000 0.000000 308.120972\n""7 1 137.260666 36.164043 1.936233 271.730042 0.003466 93979.585938 4.138164 66.843140 0.000000 0.000000 308.172363\n""8 1 137.261642 36.164043 1.960050 271.728485 0.003468 93979.523438 4.166300 67.297676 0.000000 0.000000 308.221008\n""9 1 137.262634 36.164043 1.982234 271.669006 0.003469 93875.414062 4.197948 67.724579 0.000000 0.000000 308.231415\n" ) ;
// dump the contents of the input stream so that we can see what it is
std::cout << "input stream contains:\n-------------------\n" << file.rdbuf() ;
file.clear() ; file.seekg(0) ; // clear the state and seek to the beginning of the stream
constauto vec = get_data(file) ; // extract the data in the input stream
// dump the extracted data so that we can see what it is
std::cout << "\n\nextracted data:\n-------------------\n" << std::fixed ;
for( constauto& array : vec )
{
for( double v : array ) std::cout << v << " " ;
std::cout << '\n' ;
}
}
Thank you very much to write down the code, i really need that.
The data save into file text the name is "data.txt"
so when i input the file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main() // simple test driver
{
// simulate an input file containing test data (in actual code, use std::ifstream instead)
std::iftream file("data.txt") ; //i got an error here why???
// dump the contents of the input stream so that we can see what it is
std::cout << "input stream contains:\n-------------------\n" << file.rdbuf() ;
file.clear() ; file.seekg(0) ; // clear the state and seek to the beginning of the stream
constauto vec = get_data(file) ; // extract the data in the input stream
// dump the extracted data so that we can see what it is
std::cout << "\n\nextracted data:\n-------------------\n" << std::fixed ;
for( constauto& array : vec )
{
for( double v : array ) std::cout << v << " " ;
std::cout << '\n' ;
}
}
oh i'm sorry i already try ifstream without typo but there still an error
std::ifstream file("data.txt") ;
the error
1 2 3 4
C:\Users\Student\Documents\C++\#Interpolasi program\newhelpedcpp.cpp In function 'int main()':
39 23 [Error] variable 'std::ifstream file' has initializer but incomplete type
49 30 [Error] unable to deduce 'auto&&' from 'vec'
51 25 [Error] unable to deduce 'auto&&' from 'array'