Save output in text C++

Hi ,
i want to save the output of my c ++ code to a text file which is in a particular path :
this is part of my code and I dunno where I am doing it wrong

do
    {
        for( int i = 0; i < l; ++i  )
        {
            std::cout << 1 + k * i + index << ' ';
        }
		        std::cout << std::endl;
    }
    while( next_index( index.begin(), index.end(), M ) );
std::ofstream out("C:project5\part2\out.txt");
	freopen("out.txt","w",stdout);
    cout<<"write in file";
}

please do advise on my error
thanks

cout is not stdout, they are in fact separate.

You do not need to change where cout or stdout go to, to write to a file, in any case.

See this fstream example for an example on how to write to a file stream.

yeah you should use one or the other, iostream or stdio and not mix and match, ideally.