Skip to content
Question

Q.A text file named MESSAGE.TXT contains some text. Another text file named SMS.TXT needs to be created such that it would store only the first 150 characters from the file MESSAGE.TXT. Write a user-defined function LongToShort() in C++ that would perform the above task of creating SMS.TXT from the already existing file MESSAGE.TXT.

(OR)
A text file named CONTENTS.TXT contains some text. Write a user-defined function LongWords() in C++ which displays all such words of the file whose length is more than 9 alphabets. For example : if the file CONTENTS.TXT contains : "Conditional statements of C++ programming language are if and switch" Then the function LongWords() should display the output as : Conditional statements programming
CBSECBSE Class XII Board 2019Subjective· 3mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): copy the first 150 characters of MESSAGE.TXT into SMS.TXT. Part (b): print each word of CONTENTS.TXT longer than 9 letters (Conditional statements programming).

C++ file handling uses ifstream for reading and ofstream for writing, both from <fstream>. The two alternatives share the read-then-filter pattern.

Part (a)

We must transfer only the first 150 characters. Read a character at a time; the loop stops either when 150 characters have been written or when get() fails at end-of-file.

#include <iostream>
#include <fstream>
using namespace std;

void LongToShort() {
    ifstream fin("MESSAGE.TXT");
    ofstream fout("SMS.TXT");
    if (!fin) { cout << "Cannot open MESSAGE.TXT"; return; }
    char ch;
    int count = 0;
    while (count < 150 && fin.get(ch)) {
        fout.put(ch);
        count++;
    }
    fin.close();
    fout.close();
}
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.