954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

writing an array to a file

How can i write an array to file using C++?? i need to write the whole array at once, meaing that the writing function should take a pointer to the array.
And the array is holding floating numbers..
What's that fuction name that will satisfy that? How can i do it??
Thank you..

Flawless
Newbie Poster
3 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Here this code should help.

You need to do it manually, send the function to do the saving the pointer the the array, and the number of elements in the array.

#include <fstream.h>

void saveArray(float* array, int length);

int main()
{
    float floatArray[] = { 15.25, 15.2516, 84.168, 84356};
    saveArray(floatArray, 4);

    return 0;
}

void saveArray(float* array, int length)
{
    ofstream output("output.txt");

    for(int i=0;i<length;i++)
    {
        output<<array[i]<<endl;
    }
}


Dknight764

DKnight764
Newbie Poster
2 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You