How can the Lambda expression be modified to put string in text file?

Dear all,

I have a code, with that I can show the out put on the screen. But, I want to put the output in a text file. Frankly, I read about the Lambda expression, but it is hard for me to understand its function (as I am beginner).

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// if the dimensions are constant, then you could use std::array instead of std::vector.
using Matrix = std::vector<std::vector<float>>;

void addRow(Matrix& M, const int rowNum, const int id, const float type, const float x, const float y,const float z) {
    M[rowNum][0] = id;
    M[rowNum][1] = type;
    M[rowNum][2] = x;
    M[rowNum][3] = y;
    M[rowNum][4] = z;
}

int main() {
    string line;
    float x0, y0, z0, x1, y1, z1, type;
    int NUMBER_line=0, id=0,NUMBER_line1=0,NUMBER_line3=0;
	
	ofstream secondoutput;					// the output text file
	secondoutput.open ("secondoutput.txt");	// the output text file
	
    ifstream myfile("1.lammpstrj");
    string namefile;
    if (myfile.is_open()) {
        for (int lineno = 0; getline(myfile, line) && lineno < 7; lineno++) {
            if (lineno == 2)  myfile >> NUMBER_line;
        }
        cout << " NUMBER_line: " << NUMBER_line << endl;

        Matrix Input0(NUMBER_line, std::vector<float>(5));
        for (float linenoq = 0; getline(myfile, line) && linenoq < NUMBER_line; linenoq++) {
            myfile >> id >> type >> x0 >> y0 >> z0;
            addRow(Input0, linenoq, id, type, x0, y0, z0);
			
        }


		for (int i=0; i <3; i++){
			for (int lineno = 0; getline(myfile, line) && lineno < 7; lineno++) {
				if (lineno == 2)  myfile >> NUMBER_line1;
			}
			
			
			Matrix Input1(NUMBER_line1, std::vector<float>(5));
			for (int linenoq = 0; getline(myfile, line) && linenoq < NUMBER_line1; linenoq++) {
				myfile >> id >> type >> x1 >> y1 >> z1;
				addRow(Input1, linenoq, id, type, x1, y1, z1);
				
			}
		
			Matrix Output(NUMBER_line, std::vector<float>(5));
			for (size_t row = 0; row < Output.size(); ++row) {
				for (size_t col = 0; col < Output[0].size(); ++col) {
					if (col < 2) {
						Output[row][col] = Input0[row][col];
					}
					else {
						Output[row][col] = Input1[row][col] - Input0[row][col] ;
					}
				}
			}
			
			std::for_each(Output.cbegin(), Output.cend(),
			[](auto const& row) {
				std::for_each(row.cbegin(), row.cend(),
					[](const float value) { 
						cout << value << " "; 
						secondoutput << value << " "; // the output text file
						});
				cout << endl;
				secondoutput << "\n" ; // the output text file
				 
			});
		}
		secondoutput.close();
	}
    else cout << "Unable to open file";
    return 0;
}


Could I ask to help me? I want to have a output text file like what the output writes on the screen.

Thanks in advance for any help

regards
Last edited on
lambdas are 'tiny functions' and are typically used, for the most part, as parameters to functions that accept functions. A simple example of this is std::sort which can accept a user-defined comparison operator (this can be used to sort the data in decending instead of acending order, among other things). The comparison function is typically very small (1-2 lines in most cases) and is a good candidate for a lambda.

normal, named functions are a better way to do larger, reusable tasks, like writing to a file.

Why do you want to do this in a lambda?
Dear jonnin,
I am not a programmer, just have an output from a software and want to do a post-process on it. this code can help me, but I need to put its output to another file ("secondoutput.txt"). then, I can use the new file for obtaining another results.
If you could help me, it would be appreciated.
It seems the most important matters are about the the place where I put the "secondoutput << value << " ";" and "secondoutput << "\n" ;"

I don't know where they should be. I always put "secondoutput << value << " ";" in other codes, and they work well, and give me an output text file. But now, I don't know why they don't work.
you can dump the output of a program to text directly. Is that helpful?
from the console, in both unix and windows, you can do it like this:
program > filename
unix can do that or it can skip writing a real file and just hand the data off to the next program directly (would feed cin statements I think, I don't do a lot of this).

that aside, what you have is correct, except stop trying to make a lambda from it. You don't need that here.

Just put the loops of the lambda right where you have the lambda as if normal code ...
Last edited on
yes really, that is enough for me.
I use cygwin, then I turn the C++ code to an .exe file.
you mean I just add "program > filename" at the end of my code?
then, after running the .exe file, it gives me a text file with the name of "filename".
no, not in the code.
at your windows console where you run your program, do it there:
programname.exe > filename.txt
but you can leave the .exe off and .txt is just a suggestion so you can open it easier in a text editor

the operating system does the work for you.
are you using the unix shell or a windows shell? I do cygwin too but from a windows shell.

both unix and windows provide these:
> direct the output of a program to a text file.
< direct a text file as input to the program as if typed at the keyboard
>> same as > but appends instead of overwrites the file

and unix also provides pipes but I am rusty with them. Windows may too, now, since it has picked up a posix shell of sorts. I don't know. If you want to play with this stuff we can google around on it.
Last edited on
Dear jonnin,
so many thanks for your help.
It works for me. And now I can have an output, and it shows exactly what I tried to attain.
Topic archived. No new replies allowed.