2d string array to txt

How to copy 2d string array to txt file?
1) By using a loop.
2) By using standard algorithms. For example there is standard algorithm that is named copy.:)
Last edited on
Can you be more specific about algorithm? With 2 loops not work...
1
2
std:;copy( std::begin( YourArray ), std::end( YourArray ),
                std::ostream_iterator<char *>( YourOutStream, "\n" ) );
Try the following example


1
2
3
char a[][10] = { "first", "second", "third" };

std::copy( std::begin( a ), std::end( a ), std::ostream_iterator<char *>( std::cout, "\n" ) );
I have this array...
If i put copy ( begin( X ), end( X ), ostream_iterator<char *>( cout, "\n" ) ); behind my array i get error....
expected constructor, destructor, or type conversion before '(' token

string X[14][5]= { {"Sati "," Ponedjeljak"," Utorak"," Srijeda", " Cetvrtak"},

{"7-8 "},
{"8-9 "},
{"9-10 "},
{"10-11"},
{"11-12"},
{"12-13"},
{"13-14"},
{"14-15"},
{"15-16"},
{"16-17"},
{"17-18"},
{"18-19"},
{"19-20"}};
You have array of type std::string. I showed code for character arrays. If your compiler supports the range based for statement then you can write

1
2
3
4
5
for ( const auto &a : x )
{
   for ( const auto &s : a ) std::cout << s << ' ';
   std::cout << std::endl;
}


where instead of std::cout you shall use your file stream.
Last edited on
Topic archived. No new replies allowed.