save data into an multidimentional array

Please help, i'm new in c++. I want to save my datafile into a multidimentional vector or array.
so i have this data

1
2
3
4
5
6
7
8
9
10
    1           1     137.254791      36.164043       1.663292     271.845428       0.003446   94178.367188       3.831074      62.038136       0.000000       0.000000     307.386322 
    2           1     137.255768      36.164043       1.651848     271.855011       0.003446   94184.335938       3.819067      61.833946       0.000000       0.000000     307.287109 
    3           1     137.256744      36.164043       1.640299     271.861389       0.003446   94184.398438       3.807216      61.646248       0.000000       0.000000     307.271057 
    4           1     137.257736      36.164043       1.628936     271.861908       0.003446   94173.765625       3.796037      61.468674       0.000000       0.000000     307.290863 
    5           1     137.258713      36.164043       1.886245     271.794739       0.003462   94096.921875       4.074421      65.879463       0.000000       0.000000     308.017212 
    6           1     137.259689      36.164043       1.911562     271.773621       0.003464   94057.750000       4.105538      66.371078       0.000000       0.000000     308.120972 
    7           1     137.260666      36.164043       1.936233     271.730042       0.003466   93979.585938       4.138164      66.843140       0.000000       0.000000     308.172363 
    8           1     137.261642      36.164043       1.960050     271.728485       0.003468   93979.523438       4.166300      67.297676       0.000000       0.000000     308.221008 
    9           1     137.262634      36.164043       1.982234     271.669006       0.003469   93875.414062       4.197948      67.724579       0.000000       0.000000     308.231415 
   


i want to save the column 3,4 and 5 into an multidimentional array so it'll be :
1
2
3
4
5
6
7
8
9
10
11
  137.254791      36.164043       1.663292  
  137.255768      36.164043       1.651848  
  137.256744      36.164043       1.640299  
  137.257736      36.164043       1.628936  
  137.258713      36.164043       1.886245  
  137.259689      36.164043       1.911562  
  137.260666      36.164043       1.936233  
  137.261642      36.164043       1.960050  
  137.262634      36.164043       1.982234  
  137.263611      36.164043       2.001785  
  `


please can anyone help please
for a text file... one simple way..

ofstream outfile(filename);

for(all the rows)
{
outfile << container[row][col3] << "," <<container[row][col4] << "," << container[row][col5] << endl;
}


if you name the file name.csv excel or spreadsheet program will open it correctly without extra trouble.
Last edited on
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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
    return bool( 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

    const auto 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( const auto& array : vec )
    {
        for( double v : array ) std::cout << v << "      " ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/1ddcf174b0b8e41d
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

    const auto 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( const auto& array : vec )
    {
        for( double v : array ) std::cout << v << "      " ;
        std::cout << '\n' ;
    }
}
Last edited on
There is a typo on line 4
1
2
//  std::iftream file("data.txt") ; 
std::ifstream file("data.txt") ; 


Look at the error diagnostic emitted by the compiler; it tells us what the error is:
echo && echo '=========== g++ -O3 ================' && g++ --version | grep GCC
echo && g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out 
echo && echo '=========== clang++ -O3 ================' && clang++ --version | grep clang
echo && clang++ -std=c++14  -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out

=========== g++ -O3 ================
g++ (GCC) 7.2.0

main.cpp: In function 'int main()':
main.cpp:6:10: error: 'iftream' is not a member of 'std'
     std::iftream file("data.txt") ; 
          ^~~~~~~
main.cpp:6:10: note: suggested alternative: 'ifstream'
     std::iftream file("data.txt") ; 
          ^~~~~~~
          ifstream

=========== clang++ -O3 ================
clang version 3.8.0 (tags/RELEASE_380/final 263969)

main.cpp:6:17: error: expected ';' after expression
    std::iftream file("data.txt") ; 
                ^
                ;
main.cpp:6:10: error: no member named 'iftream' in namespace 'std'
    std::iftream file("data.txt") ; 
    ~~~~~^
main.cpp:6:18: error: use of undeclared identifier 'file'
    std::iftream file("data.txt") ; 
                 ^
3 errors generated

http://coliru.stacked-crooked.com/a/38746ca8d2a3fc4f
Last edited on
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'

what i supposed to do?
> variable 'std::ifstream file' has initializer but incomplete type

#include <fstream>

The other errors should go away; if they don't post your code along with the complete text of the error diagnostic.
Topic archived. No new replies allowed.