c++ has to produce text file

hi frns,
my problem is i have to write C++ code and that has to produce a text file as output..which command in C++ will do that..pls solve my problem if any one can do...
Try using fstream
You'll need to include it first tho, using:
#include <fstream>

http://www.cplusplus.com/reference/iostream/fstream/
Yes, fstream will do it. As an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <fstream>
#include <iostream>

int main() {
    std::ifstream myFile; // Create ofstream (output filestream) object
    myFile.open("myFile.txt"); // Open a file
    
    if (myFile.is_open()) { // Check file is open
        while(!(myFile.eof())) { // Check that we haven't read past the last line
            getline(myFile, line); // Get each line
            std::cout << line; // Print the line
        }
    }

    myFile.close(); // Close file
    return 0;
}
Altho, his assignment requires him to create a textfile as output :)
Topic archived. No new replies allowed.