Write cout value to text file

The for loop is to randomize 65 - 90 and using (char) to display them in alphabet A-Z. I can have the display in alphabet A-Z in the console but I am not able to write them in text file.

for(i = 0; i < 26; i++) cout  
51.4k 13 13 gold badges 106 106 silver badges 143 143 bronze badges asked Jul 16, 2012 at 2:38 newbieprogrammer newbieprogrammer 868 7 7 gold badges 26 26 silver badges 47 47 bronze badges Ahem. The code for both situations should be same. It isn't. (Hint: parentheses matter) Commented Jul 16, 2012 at 2:42 (2nd Hint: the for line matters). Commented Jul 16, 2012 at 2:44 This code is in such a poor state I refuse to take a look. Please organize it and post again. Commented Jul 16, 2012 at 4:23

1 Answer 1

Since std::ofstream inherits from the same type as std::cout is an instance of (std::ostream) their interfaces are the same, some methods are added to the file-handling stream but other than that they are interchangeable.

With the above said the only thing required from your part is that you are actually implementing the functionality the same way in both cases, currently: (by reading your snippets) you are not.

  1. the std::ofstream example doesn't have any loop
  2. the order of evaluation is different (since you don't use () ) in the std::ofstream snippet

To make sure you use the same implementation no matter if you are writing to a file or standard-output you could wrap the code in a function, making it accept a reference to std::ostream as in the below snippet.

#include #include
void do_whatever (std::ostream& output_stream) < int letter[] = < 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 >; for (int i =0; i
int main (int argc, char *argv[])