Question

Write a C++ Program to do following: 1. Read every character from file char.txt using "get"...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Write a C++ Program to do following: 1. Read every character from file char.txt using "get"...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Retrieve program randomAccess.cpp and the data file proverb.txt from the Lab 14 folder. The code is as follows: #include...

    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...

  • *Java* You will write a program to do the following: 1. Read an input file with...

    *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 !...

    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...

    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...

    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]...

  • I need to complete a fill-in-the-blank C++ program using DEV C++, code needs to stick to program...

    ************************* 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...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    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...

  • Using c++ ) You will write a program that will do the following: prompt the user...

    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 C program which will display the contents of a file in base-16 (hexadecimal) and...

    Write a C program which will display the contents of a file in base-16 (hexadecimal) and in ASCII. Complete the following tasks: Obtain the name of the input file from the command line. If the command-line is “./hexdump xxx.bin” then argv[1] will contain “xxx.bin”. Open the file for binary input Print the entire file, 16-bytes per line. Each line should begin with an 8-digit hexadecimal offset into the file. This is the count of the bytes that you have already...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT