Write a method called writeNames that takes two parameters an array of Strings (
ID: 3824604 • Letter: W
Question
Write a method called writeNames that takes two parameters an array of Strings (nameArray) and a String (fileName), and returns nothing. Write the array of Strings (names) to a text file named using the fileName parameter. Make sure to catch any Exception that might be thrown. Write a method called readNames that takes a String parameter (fileName) and returns an Array ist of Strings (nameArrayList). The method reads the text file (fileName) and populates the ArrayList with an element for each line in the file. The method returns the populated ArrayList. Make sure to catch any Exception that might be thrown.Explanation / Answer
Hi,
I have written the code for part one which gives me some weird kind of error. However in another compiler it is compiling well. Please try it once-
C++ Code-
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void writeNames(string[] ,string);
int main () {
string filename="example.txt";
string x[] = {"vinay","sonu","kkkkkkkkkk","mohan","molla"};
writeNames("vinay","example.txt");
return 0;
}
void writeNames(string fff[],string &filename){
ofstream myfile (filename);
if (myfile.is_open())
{
for(int count = 0; count < 5; count ++){
myfile << fff[count] << " " ;
}
myfile.close();
}
else cout << "Unable to open file";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.