reading and writing to a file

This is my first time trying to get a program to read and write to a file. First I created a file of random numbers in notepad and saved it to dev-cpp bin.
I named it in.txt. In the get_file function, the program asks for the file. I tried in in.txt and in.txt.txt. it fails. Do I need to give it some direction. Is the function correct. Thanks







#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

void get_file(ifstream& fin, ofstream& fout);
/* to get, open, and test the files for input and output data.*/
void find_mean(ifstream& fin, ofstream& fout, double& mean, double list[], int& count);
/* to find the average of all numbers in the file, write the mean to
the output file, and return the count to the main. */
void deviation (ifstream& fin, ofstream& fout, double list[], int count, double mean);
/*to geet the standard deviation of the group of numbers and write it to the output file. */
void sort_array(double list[], int count);
/*to sort the numbers in the array in ascending order to get the median*/
void median(double list [], int count, ofstream& fout);
/*to get the median of the list and send it to the file*/



int main(int argc, char *argv[])
{
int count = 0;
double mean, list[1000];
ifstream fin;
ofstream fout;
get_file (fin, fout);
find_mean (fin, fout, mean, list, count);
deviation (fin, fout, list, count, mean);
sort_array(list, count);
median (list, count, fout);
system("PAUSE");
return EXIT_SUCCESS;
}
void get_file (ifstream &fin, ofstream &fout)
{
char in_file[40], out_file[40];
cout << "What is the name of the file containing data to be analyzed?\n";
cin >> in_file;
fin.open(in_file);
if (fin.fail())
{
cout << "Input file failed.\n";
exit(1);
}
cout << "What is the name of the file where you would like to store data?\n";
cin >> out_file;
fout.open(out_file);
if (fout.fail())
{
cout << "imput file failed.\n";
exit(1);
}
}
void find_mean (ifstream& fin, ofstream& fout, double& mean, double list[], int& count)
{
count = 0;
double next, sum = 0;
fout.setf(ios::fixed);
fout.setf(ios::showpoint);
fout.precision(2);
while (fin >> next)
{
list[count] = next;
count++;
sum += next;
}
mean = (sum/count);
fout << "MEAN: " << mean << endl;
}
void deviation (ifstream& fin, ofstream& fout, double list[], int count, double mean)
{
fout.setf(ios::fixed);
fout.setf(ios::showpoint);
fout.precision(2);
double variance;
double sum = 0;
for (int i = 0; i < count; i++)
{
sum += pow((list[i] - mean), 2.0);
}
variance = sum/count;
fout << "Standard deviation: " << sqrt(variance) << endl;
}
void sort_array(double list[], int count)
{
double temp =0;
for (int i=0; i < (count - 1); i++)
{
int j = i+1;
for (; j < count; j++)
{
if (list[i]>list[j])
{
temp = list[i];
list [i] = list [j];
list [j] = temp;
}
}
}
}
void median(double list [], int count, ofstream& fout)
{
fout.setf(ios::fixed);
fout.setf(ios::showpoint);
fout.precision(2);
double median;
if (count%2 == 0)
median = ((list[count/2] + list [(count/2)-1])/2);
else
median = (list[count/2]);
fout << "Median: " << median << endl;
}



1
2
3
4
5
6
7
void get_file (ifstream &fin, ofstream &fout)
{
char in_file[40], out_file[40];
cout << "What is the name of the file containing data to be analyzed?\n";
cin >> in_file;
fin.open(in_file);
if (fin.fail())


To my understanding, in the line
fin.open(in_file);
you would have to change in_file to a C std string using
in_file.c_str()

I have never tried this, but it should work. If it doesn't, something along the lines of that should work. You just have to changed the string to a C std string if you want to open a file using a variable name.
Topic archived. No new replies allowed.