CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2011
    Posts
    13

    help with write to .txt file

    Hi everyone,
    need some help, this function doesnt work for me and I dont know why, maybe you can help me.

    Code:
    void menu4(double array[], int n) {
        ofstream results("results.txt"); 
        for (int i = 0; i < n ; i++)
        {
            results << array[i] << " ";
            cout << array[i] << endl;
        }
        results << endl;
    }
    'cout' is printing the array elements, but in file i dont see nothing

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: help with write to .txt file

    You're not closing the output file. I wouldn't bet on the data being written out properly without that.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: help with write to .txt file

    The closes "automatically" when the ofstream object goes out of scope.

    Viggy

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: help with write to .txt file

    Quote Originally Posted by MrViggy View Post
    The closes "automatically" when the ofstream object goes out of scope.
    Oh, thanks. I'll try to memorize tha now.

    So I think first the OP should check whether the file gets opened correctly at all before writing to it. Perhaps there's some sort of permission issue?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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

Featured