C++ Please define 3 public member functions called loadFile (.txt), which reads
ID: 3670231 • Letter: C
Question
C++
Please define 3 public member functions called loadFile (.txt), which reads from the provided file and fills the arrays with the provided information in the file.
->my program is using header files and cpp files.
I don't know HOW to do this. Would those three member functions have the same name? Why would it need to load the file 3 times? My arrays are called Academic and recreation if that helps.
what would that actually look like meaning the definition in the h file and the implementation in main?
Explanation / Answer
Here i have created a c++ program having function loadFile() to load the string content of a file in array. Then i have printed the array. For the code given below create a text file named "firstfile" and put two or three lines of text. With the program given below, content of that file will be stored in array. According to me single function is sufficient. If you want to do three different thing, then create three methods, name of the methods can be same but parameters of the function should be different.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class readFile //created class for readFile
{
public:
void loadFile(); //method for loading a file having strings and save its contents to array
};
void readFile::loadFile()
{
int size = 1024;
char * fileArray = new char[size];
int pos = 0;
ifstream fin("firstfile.txt");
if(fin.is_open())
{
cout << "File opened" << endl;
while(!fin.eof() && pos < size)
{
fin.get(fileArray[pos]);
pos++;
}
fileArray[pos-1] = '';
cout<<"Data in array is as follows:"<<endl<<endl;
for(int j = 0; fileArray[j] != ''; j++)
{
cout << fileArray[j];
}
}
else
{
cout<<"File not opned"<<endl;
}
}
int main()
{
readFile f1;
f1.loadFile();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.