Skip to main content
GameDev.net gamedev.net
🔒 Locked

fstream

Started by counterrabbit Oct 25, 2005 at 10:01 AM 3 replies 550+ views
Original Post
counterrabbit
counterrabbit
hey here say i had code that looked like int array[20]; // fill this array with 20 values somewhere in the program for(int i = 0; i<20; i++) { int newarry[20] newarray = array; } so really all whats happening is that one array is pasing its contcts to another array how do i write the contents that are being put into the newarray to a text file? thanks
ToohrVyk
ToohrVyk
No, you are not moving the contents from one array to another. The "newarray" variable is initialized 20 times (one for each loop iteration), and each iteration only one element of this array is set to something. Therefore, you are moving each element from the array to a different array.

Now, about your question:

I want to write data to a text file

{  std::ofstream out( "file.txt" );  std::ostream_iterator<int> it( out, " " );  std::copy( array, array + 20, it );}// Also, I do believe microsoft extensions allow you to do:{  std::copy( array, array + 20,    std::ostream_iterator<int>( std::ofstream( "file.txt" ), " " ) );}
counterrabbit
counterrabbit
yeah sorry i have to type quickly at work,
Zahlman
Zahlman
Microsoft extensions shouldn't be required just to create the ostream_iterator inline. Since the parameters are not a set that could all be interpreted as typenames (i.e. either default-constructed things or things recursively constructed from such), there is no way for that to be interpreted as a function declaration (if that's what you were thinking of).
SiCrane
SiCrane
Quote:
Original post by Zahlman
Microsoft extensions shouldn't be required just to create the ostream_iterator inline. Since the parameters are not a set that could all be interpreted as typenames (i.e. either default-constructed things or things recursively constructed from such), there is no way for that to be interpreted as a function declaration (if that's what you were thinking of).


Actually I believe the MS extension is necessary for binding an rvalue to a non-const reference.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.