Saturday, March 12, 2011

CPP - Writing To File

To write for a file, type:
ofstream outfile("lola.txt");
char names[100] = "Lady Gaga";

for(int i=0; i<100; i++){

We do like that because argument for put is a char, not a string;
Or you can do with write
char names[100] =  "Lady Gaga shares with Luna Maya";
outfile.write(names, 100)

Or the most convinient way is do like what cout does:
char names[100] = "Lady Gaga And The Other One";
outfile << names;

Here's a code snippet of the formatted data:
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]){
    system("color 2f");

    ofstream outfile("e:\\lola\\hello.txt");

    string data = "Lady Gaga\tNew York\t24\n";
    data = data + "Luna Maya\tDenpasar\t27\n";
    data = data + "Bill Clinton\tNew York\t64\n";

    outfile << data << endl;

    return 0;


No comments:

Post a Comment