Write a C++ Program to do following:
1. Read every character from file char.txt using "get" and write it to a file char.bak using "put"
2. Read every second character from file char.txt using "get" and write it to a file charalternate.txt using "put" and "ignore"
3. Take input from user for offset and number of characters. For example if user enters 5 for offset and 10 for number of characters, then your program should copy 10 bytes from file char.txt from position 5 and write it a file named byteswritten.txt.
Question 1:
Here is code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("char.txt");
ofstream outfile ("char.bak");
if (!in)
{
cout << "Cannot open file.\n";
return 1;
}
char c;
while (in)
{
in.get(c);
if (in)
outfile.put(c);
}
in.close();
outfile.close();
return 0;
}
char.txt:
nibh in quis justo maecenas rhoncus aliquam lacus morbi quis
tortor id nulla ultrices aliquet
vivamus in felis eu sapien cursus vestibulum proin eu mi nulla
ac
curabitur convallis duis consequat dui nec nisi volutpat eleifend
donec ut dolor morbi
proin risus praesent lectus vestibulum quam sapien varius ut
blandit non interdum in ante vestibulum ante
vivamus vel nulla eget eros elementum pellentesque quisque porta
volutpat erat quisque erat eros viverra
char.bak:

Question 2:
Here is code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("char.txt");
ofstream outfile ("char.bak");
if (!in)
{
cout << "Cannot open file.\n";
return 1;
}
char c;
while (in)
{
in.get(c);
if (in)
outfile.put(c);
in.ignore(1);
}
in.close();
outfile.close();
return 0;
}
char.txt:
nibh in quis justo maecenas rhoncus aliquam lacus morbi quis
tortor id nulla ultrices aliquet
vivamus in felis eu sapien cursus vestibulum proin eu mi nulla
ac
curabitur convallis duis consequat dui nec nisi volutpat eleifend
donec ut dolor morbi
proin risus praesent lectus vestibulum quam sapien varius ut
blandit non interdum in ante vestibulum ante
vivamus vel nulla eget eros elementum pellentesque quisque porta
volutpat erat quisque erat eros viverraa
charalternate.txt:
nb nqi ut acnsrocsaiumlcsmriqi otri ul lrcsaiutvvmsi ei uspe
ussvsiuu ri um ul ccrbtrcnalsdi osqa u e iivlta lieddncu oo
ob
ri iu reetlcu etblmqa ainvru tbadtnnitru nat etblmat
iau e ul gteo lmnu elneqeqiqepravlta rtqiqeea rsvvra
Question 3:
Here is code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("char.txt");
ofstream outfile("byteswritten.txt");
if (!in)
{
cout << "Cannot open file.\n";
return 1;
}
int offset, bytes;
cout << "Enter offset : ";
cin >> offset;
cout << "Enter number of bytes : ";
cin >> bytes;
in.ignore(offset); // Ignore up to 10 characters or until first space is found.
char c;
int count = 0;
while (in)
{
in.get(c);
if (in)
outfile.put(c);
count++;
if (count >= bytes)
break;
}
in.close();
outfile.close();
return 0;
}
Output:

char.txt:
nibh in quis justo maecenas rhoncus aliquam lacus morbi quis
tortor id nulla ultrices aliquet
vivamus in felis eu sapien cursus vestibulum proin eu mi nulla
ac
curabitur convallis duis consequat dui nec nisi volutpat eleifend
donec ut dolor morbi
proin risus praesent lectus vestibulum quam sapien varius ut
blandit non interdum in ante vestibulum ante
vivamus vel nulla eget eros elementum pellentesque quisque porta
volutpat erat quisque erat eros viverraa
byteswritten.txt:
in quis ju
Write a C++ Program to do following: 1. Read every character from file char.txt using "get"...
Retrieve program randomAccess.cpp and the data file proverb.txt from the Lab 14 folder. The code is as follows: #include <iostream> #include <fstream> #include <cctype> using namespace std; 327 int main() { fstream inFile("proverb.txt", ios::in); long offset; char ch; char more; do { // Fill in the code to write to the screen // the current read position (with label) cout << "Enter an offset from the current read position: "; cin >> offset; // Fill in the code to move...
using C codding language, Write a program for following tasks: 1. get string (character array) from user and save it in char input. Assume maximum size of input is going to be 50 characters. 2. print out given input on the screen. 3. print out size of given input. (Hint: sizeof function)
*Java*
You will write a program to do the following: 1. Read an input file with a single line of text. Create a method named load_data. 2. Determine the frequency distribution of every symbol in the line of text. Create a method named 3. Construct the Huffman tree. 4. Create a mapping between every symbol and its corresponding Huffman code. 5. Encode the line of text using the corresponding code for every symbol. 6. Display the results on the screen....
implicit none ! Declare File Read Variables CHARACTER(255) :: Line INTEGER :: CP ! Character position in Line INTEGER :: File_Read_Status ! Declare character constants CHARACTER :: Tab > ACHAR(1) CHARACTER :: Space = " " ! Read all lines in the file DO READ(*,'(A)',iostat = File_Read_Status) Line ! Exit if end of file IF (File_Read_Status < 0) EXIT ! Skip leading white space DO CP = 1, LEN_TRIM(Line) IF...
Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...
Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...
************************* proverb.txt. **********************************Now Is The Time fOr All GoOd Men to come to the aid of their Family*************************************************************************the sample run is as follows:Sample Run:The read position is currently at byte 0Enter an offset from the current position: 4The character read is IIf you would like to input another offset enter a Y yThe read position is currently at byte 5Enter an offset from the current position: 2The character read is TIf you would like to input another offset enter a...
Using c++
) You will write a program that will do the following: prompt the user enter characters from the keyboard you will read the characters until reading the letter O' You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ' Example inpu mJ0*5/tx1@3qcx0 The 'Q'should be included when computing the statistics properties of the input. Since characters are...
Write a program in C++ to read a large piece of text from the user (multiple lines) until the user inputs EOF. Assume the input only contains alphabets, numbers, commas and periods. The program should first compute the number of occurrences of each character in the input. Next, the program displays a sorted list (in descending order) of each character and the corresponding number of occurrences of that character. The sorting should be performed on the counts and not the...
Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...