I have a program where i have to use partial array intialization to read data from a file and place it into an array...there is no example of this in my class book and i'm confused!

Recommended Answers

All 4 Replies

your thread subject is "Need help writing an array to a file! "
and youre question body is about writing to a array from reading a file.

Both things you can done with the STL ifstream and ofstream classes.
here are some examples.

http://www.cplusplus.com/doc/tutorial/files/

and also like you use the << and >> operator to write and get input
from the stdin and stdout you can use them for ifstream and ofstream objects. for example.

int i ;
ifstream file ("example.bin");
// read an int
file >> i ;

Im not the best by far or good but I can help with what I know...
Also writing to a file and reading from a file are different steps just a little bit Ill provide both and Ill assume you have to read from a pre named file

//declare are variables
ifstream fin;
int num; //In this instance lets say we are reading in integers
int arr[100]; //Array has max of 100 values
int i; //Our iterator to keep count of items in file

fin.open("file.txt"); //open whatever file 

//Create a loop and imply a counter to count all items in the file 

while(!fin.eof() && i < 100)
{
   fin>>num;
   i++;
}

//The file well stop reading in values when it reaches end of file or 100 values for the arrays size

//Now that you have that, your array is filled and you can print out to console with a for loop

 // A variable of j is intialized to loop the output unitl it reaches "i" which is the count of the items in file and the current size of the array

for(int j = 0; j < i; j++)
{
   cout<<arr[i]<<endl;
}

fin.close();
return 0;

//And to write out to file the inside to the close after you read from the file

fout.open("name of your file here");

for(int j = 0; j < i; j++)
{
   fout<<arr[i]<<endl;
}

fout.close();

But all in all you need to google better they are TONS of material on this or search this site

sorry about the title mix up i mean writing a file to an array....this is what my program should do: calculates the mean and standard deviation of intergers stored in a file into an array. The output should be of type float and should be properly labeled and formated to two decimal places. This is the program i have written:

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;


const int Max_Numbers = 100;


int main()
{
ifstream arraydata;
float mean, stdv, total, data[Max_Numbers];
int numdata = 0, i;
string file;


cout<< "What data file do you want to use to find the mean and standard deviation?  ";
cin>>file;


cout<<endl<<endl;


arraydata.open(file.c_str());


while(!arraydata.eof() && arraydata>>data[numdata])
{
numdata++;
}


for(i=0; i<=numdata; i++)
{
total+=data;
}


arraydata.close();


mean=total/numdata;


stdv=sqrt(((pow(total,2.0))-((1.0/numdata)*(pow(total,2.0))))/(numdata-1.0));


cout<<"The mean is "<<mean<<"and the standard is "<<stdv<<"."<<endl;


return 0;


}

Please use code tags--it is very difficult to look at code that is not formatted.

You posted your code, but you haven't indicated a problem. Is there a particular problem you are noticing (compiling or at runtime)?

You should also attach your input data file so when someone wants to help you they can have everything they need to get started.

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.