C++ FILE I/O program implementation You may program the below as individual prog
ID: 3560223 • Letter: C
Question
C++ FILE I/O program implementation
You may program the below as individual programs or in OOP style using a class and methods.
(20 points total)
1. Write a program which will read an input file (text file ONLY) and write output to another file. Manage file i/o errors.
2. Given an input text file, write a program which will double space the file. For example if the file has
INPUT:
Information Society band still rocks
Java handles Generics better than C++ templates.
OUTPUT: (ie double spaced will be)
Information Society band still rocks
Java handles Generics better than C++ templates.
END OF OUTPUT:
3. Given a input file with text, write a program to read from the file, alternate lines (ie either line 1 3 5 etc or line 2 4 6 etc) and print the output to console aka standard output.
4. Write a function which will take a pointer to a string as an argument. This function will recursively reverse-print the string character by character. Demo the use of this function in a program.
Explanation / Answer
//You may program the below as individual programs or in OOP style using a class and methods.
//1. Write a program which will read an input file (text file ONLY) and write output to another file. Manage file i/o errors.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
if(!infile)
{
cout<<"unable to open file .so exiting from program " << endl;
return 0;
}
string str;
while(!infile.eof())
{
getline(infile,str);
outfile << str << endl;
}
infile.close();
outfile.close();
return 0;
}
// 2. Given an input text file, write a program which will double space the file. For example if the file has
// INPUT:
// Information Society band still rocks
// Java handles Generics better than C++ templates.
// OUTPUT: (ie double spaced will be)
// Information Society band still rocks
// Java handles Generics better than C++ templates.
// END OF OUTPUT:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
if(!infile)
{
cout<<"unable to open file .so exiting from program " << endl;
return 0;
}
string str;
while(!infile.eof())
{
getline(infile,str);
for(int i=0; i<str.length(); i++)
{
if(str[i] == ' ') outfile << str[i] << str[i];
else outfile << str[i];
}
outfile << endl;
}
infile.close();
outfile.close();
return 0;
}
// 3. Given a input file with text, write a program to read from the file, alternate lines (ie either line 1 3 5 etc or line 2 4 6 etc)
// and print the output to console aka standard output.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
if(!infile)
{
cout<<"unable to open file .so exiting from program " << endl;
return 0;
}
string str;
int i=0;
while(!infile.eof())
{
i++;
getline(infile,str);
if(i%2!=0) cout << str << endl;
}
infile.close();
return 0;
}
// 4. Write a function which will take a pointer to a string as an argument. This function will recursively reverse-print the string character by character.
// Demo the use of this function in a program.
#include<iostream>
#include<cstring>
using namespace std;
void print_reverse(char *str)
{
if(*str=='') return;
else
{
print_reverse((str+1));
cout <<*str;
}
}
int main()
{
print_reverse("kumar");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.