Results 1 to 4 of 4

Thread: Writing to a file (Im a newbie :P)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Posts
    183

    Writing to a file (Im a newbie :P)

    Well Im new to C++ and I am using "bloodShed Dev C++" as my compiler. I would like to write to a text file "C:\anser.txt" but the I cant get the code I was given to work

    Could someone please tell me how to write to a text file? )
    When you ask a question, try and answer a few

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    #include <fstream.h>
    #include <stdlib.h>
    #include <iostream.h>

    int main()
    {
    ofstream outfilie("answer.txt", ios::trunc);
    if(outfile.fail())
    {
    cout<<"File Error"<<endl;
    exit(1);
    }
    outfile<<"Test"<<endl;

    return 0;
    }

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Posts
    183

    woohoo

    It works! Thanks so berry much
    When you ask a question, try and answer a few

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by SteveCRM
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
        ofstream outfile("answer.txt", ios::trunc);
    
        if(!outfile) {
            cerr << "File Error" << endl;
            return -1; // or 1 or something non-zero
        }
    
        outfile << "Test" << endl;
    }
    Ummmm...see the corrections
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width