What am I missing and how should I implement this idea? I don't want my int main cluttered up with file stream stuff. Here's my attempt:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int MAX_STRING_SIZE=30;
void readfile(char filename[], istream& streamname);
/* takes in a file name represented by a c-string and
assigns it to an input file stream. requires header
file <fsream>*/
void getstring(char givenname[MAX_STRING_SIZE]);
/* takes c-string given by user and stores it in the
passed character array*/
void printfile(istream& streamname);
/* uses cout to print contents of the file passed
to the function*/
int main()
{
ifstream fin;
char filename[MAX_STRING_SIZE];
readfile(filename, fin);
return 0;
}
void readfile(char filename[], istream& streamname)
{
streamname.open(filename);
if (streamname.fail())
cout << "Failed to open file." << endl;
else
cout << "File opened succesfully." << endl;
return;
}
void printfile(istream& streamname)
{
return;
}
void getstring(char givenname[MAX_STRING_SIZE])
{
cin.getline(givenname,MAX_STRING_SIZE,'\n');
}

New Topic/Question
Reply



MultiQuote




|