I don't know about the second part, but to write to a text file...

Code:
#include <fstream.h>

int main()
{
    ofstream out("myfile.txt");
    out << "test" << endl;
    out << "another line";
    // close the file here, but I forgot how
    return 0;
}
Note that this (I think) only works in C++, not C.