How to write the data in txt file into other txt file

I am working on reading the data from one file "file1.txt" having format

0
0.0003
0.0006
-0.0130
-0.5542
-0.2587
2.2455e-04 -7.9772e-05 2.7506e-05
-7.9772e-05 4.3190e-05 -1.7369e-05
2.7506e-05 -1.7369e-05 3.0509e-05
0.03
0.0003
0.0006
-0.0147
-0.5546
-0.2560
2.2455e-04 -7.9772e-05 2.7506e-05
-7.9772e-05 4.3190e-05 -1.7369e-05
2.7506e-05 -1.7369e-05 3.0509e-05
0.06
0.0003
0.0006
-0.0163
-0.5551
-0.2533
2.2455e-04 -7.9772e-05 2.7506e-05
-7.9772e-05 4.3190e-05 -1.7369e-05
2.7506e-05 -1.7369e-05 3.0509e-05

and want to save in other file "file2.txt" with following format

0 -0.0130 2.2455e-04 -7.9772e-05 2.7506e-05
0.0003 -0.5542 -7.9772e-05 4.3190e-05 -1.7369e-05
0.0006 -0.2587 2.7506e-05 -1.7369e-05 3.0509e-05
0.03 -0.0147 2.2455e-04 -7.9772e-05 2.7506e-05
0.0003 -0.5546 -7.9772e-05 4.3190e-05 -1.7369e-05
0.0006 -0.2560 2.7506e-05 -1.7369e-05 3.0509e-05
0.06 -0.0163 2.2455e-04 -7.9772e-05 2.7506e-05
0.0003 -0.5551 -7.9772e-05 4.3190e-05 -1.7369e-05
0.0006 -0.2533 2.7506e-05 -1.7369e-05 3.0509e-05

I am working on C++ programming. I know its simple question and i have also given a many try to solve it but failed. If any one can help, i will be thankful.

Thank You
explain both formats
what's the meaning of the fields and how they are arranged in file2
Weird format ...
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
#include <iostream>
#include <istream>
#include <ostream>
#include <array>

std::istream& load(std::istream& is, std::array<std::array<float, 5>, 3>& v) {
    is >> v[0][0];
    is >> v[1][0];
    is >> v[2][0];

    is >> v[0][1];
    is >> v[1][1];
    is >> v[2][1];

    for (size_t i = 0; i < 3; ++i)
        for (size_t j = 2; j < 5; ++j)
            is >> v[i][j];

    return is;
}

std::ostream& save(std::ostream& os, std::array<std::array<float, 5>, 3>& v) {
    for (size_t i = 0; i < 3; ++i) {
        for (size_t j = 0; j < 5; ++j)
            os << v[i][j] << ' ';
        os << '\n';
    }

    return os;
}

int main() {
    std::array<std::array<float, 5>, 3> values;
    while (load(std::cin, values))
        save(std::cout, values);
}
Last edited on
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
63
64
65
66
67
68
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

struct Data
{
   double x, y, z;
   double u, v, w;
   double Ax[3], Ay[3], Az[3];
};

istream & operator >> ( istream &in, Data &d )
{
   in >> d.x >> d.y >> d.z >> d.u >> d.v >> d.w;
   in >> d.Ax[0] >> d.Ax[1] >> d.Ax[2];
   in >> d.Ay[0] >> d.Ay[1] >> d.Ay[2];
   in >> d.Az[0] >> d.Az[1] >> d.Az[2];
   return in;
}

ostream & operator << ( ostream &out, const Data &d )
{
   #define SP << " " <<
   out << d.x SP d.u SP d.Ax[0] SP d.Ax[1] SP d.Ax[2] << '\n';
   out << d.y SP d.v SP d.Ay[0] SP d.Ay[1] SP d.Ay[2] << '\n';
   out << d.z SP d.w SP d.Az[0] SP d.Az[1] SP d.Az[2] << '\n';
   return out;
}

int main()
{
// ifstream in( "file1.txt" );
// ofstream out( "file2.txt" );
   stringstream in(
      "0                                 \n"
      "0.0003                            \n"
      "0.0006                            \n"
      "-0.0130                           \n"
      "-0.5542                           \n"
      "-0.2587                           \n"
      "2.2455e-04 -7.9772e-05 2.7506e-05 \n"
      "-7.9772e-05 4.3190e-05 -1.7369e-05\n"
      "2.7506e-05 -1.7369e-05 3.0509e-05 \n"
      "0.03                              \n"
      "0.0003                            \n"
      "0.0006                            \n"
      "-0.0147                           \n"
      "-0.5546                           \n"
      "-0.2560                           \n"
      "2.2455e-04 -7.9772e-05 2.7506e-05 \n"
      "-7.9772e-05 4.3190e-05 -1.7369e-05\n"
      "2.7506e-05 -1.7369e-05 3.0509e-05 \n"
      "0.06                              \n"
      "0.0003                            \n"
      "0.0006                            \n"
      "-0.0163                           \n"
      "-0.5551                           \n"
      "-0.2533                           \n"
      "2.2455e-04 -7.9772e-05 2.7506e-05 \n"
      "-7.9772e-05 4.3190e-05 -1.7369e-05\n"
      "2.7506e-05 -1.7369e-05 3.0509e-05 \n"
   );

   ostream & out = cout;

   for ( Data d; in >> d; ) out << d;
}

Another version:
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
#include <iostream>
#include <fstream>

void output_to_file(std::ofstream &, double [], int [], size_t );

int main ()
{
    std::ifstream infile ("input.txt");
    std::ofstream outfile ("output.txt");
    
    double number;
    double data[15]{0};
    
    int format[]{0,3,6,7,8, 1,4,9,10,11, 2,5,12,13,14};
    size_t length = sizeof(format)/sizeof(int);
    
    int count = 0;
    int index = 0;
    
    if (infile.is_open() && outfile.is_open())
    {
        while (infile >> number)
        {
            index = count % length;
            data[index] = number;
            
            count++;
            if(count % length == 0)
            {
                output_to_file(outfile, data, format, length);
            }
        }
        infile.close();
        outfile.close();
    }
    else std::cout << "Unable to open file(s)";
    
    return 0;
}

void output_to_file(std::ofstream &anOutFile, double anArray[], int aFormat[], size_t aLimit)
{
    int count = 0;
    
    for (size_t index = 0; index < aLimit; index++)
    {
        anOutFile << anArray[ aFormat[index] ] << ' ';
       
        count ++;
        if(count % 5 == 0)
            anOutFile << '\n';
    }
}
Topic archived. No new replies allowed.