Document1-Word Layout References Mailings Review View A·?¥A |tz 2.-\" ||T Normal
ID: 3917644 • Letter: D
Question
Document1-Word Layout References Mailings Review View A·?¥A |tz 2.-" ||T Normal 1 No Spac Heading 1 Heading 2- Title Styles 9.8 Lab Exercise Ch.9b: File I/O & Interleave Write this code in Visual Studio and upload your Source.cpp file for checking. (1) Write a program to prompt the user for an output file name and 2 input file names. name of any file which has an error, and exit if an error occurs opening any of the 3. For example, (user input shown in caps in first line) Enter first input file name: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T Enter first input file name: F1.TXT Enter second input file name: F2.TXT Enter output file name: OUT.TXT Error opening output file: oUT.TXT The program should check for errors in opening the files, and print the (2) As it reads the input files, it should interieave the words of the files to write the output file and.alse to write the interleaved words on standard output (the screen). It should continue until it reaches the ends of both files, as they may be unequal lengthsExplanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include<iostream>
#include<fstream>
using namespace std;
int main(){
//initializing required variables
char filename[50];
ifstream inFile1,inFile2;
ofstream outFile;
//looping until user enters a valid input file1 name
while(true){
cout<<"Enter first input file name: ";
cin>>filename;
inFile1.open(filename);
if(!inFile1){
cout<<"Error opening input file "<<filename<<endl;
}else{
//valid, end of loop
break;
}
}
//looping until user enters a valid input file2 name
while(true){
cout<<"Enter second input file name: ";
cin>>filename;
inFile2.open(filename);
if(!inFile2){
cout<<"Error opening input file "<<filename<<endl;
}else{
//valid, end of loop
break;
}
}
//looping until user enters a valid output file name
while(true){
cout<<"Enter output file name: ";
cin>>filename;
outFile.open(filename);
if(!outFile){
cout<<"Error opening output file "<<filename<<endl;
}else{
break;
}
}
string word1,word2;
//looping until both files are read completely
while(true){
//trying to fetch a word from file1
if(inFile1>>word1){
//displaying and writing to output file
cout<<word1<<" ";
outFile<<word1<<" ";
}else{
//end of file 1
word1="";
}
//similarly fetching a word from other file
if(inFile2>>word2){
cout<<word2<<" ";
outFile<<word2<<" ";
}else{
word2="";
}
if(word1=="" && word2==""){
//both files has been read completely, end of loop
break;
}
}
//closing all files
inFile1.close();
inFile2.close();
outFile.close();
return 0;
}
/*OUTPUT*/
Enter first input file name: abababa.ttt
Error opening input file abababa.ttt
Enter first input file name: file1.txt
Enter second input file name: 99jshdn
Error opening input file 99jshdn
Enter second input file name: wwwe
Error opening input file wwwe
Enter second input file name: file2.txt
Enter output file name: out1.txt
copy some and then interleave these 2 text files. without losing any words.
/*file1.txt*/
copy and
interleave
2 files.
/*file2.txt*/
some then
these text without losing any words.
/*out1.txt*/
copy some and then interleave these 2 text files. without losing any words.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.