Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Write a program to read a file called file.txt, which contains an arbitrary
- number of characters. You have to create a new file named filea.txt that have
- all the digits in file.txt replaced with the symbol *.
- The text file can contain: This file contains some random numbers like 1 and 3.
- And can have other numbers like 4566 and 563454. A sample filea.txt, created by the program,
- for the above input, is shown below: This file contains some random numbers like * and *.
- And can have other numbers like **** and ******.
- */
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- ofstream oft; //creating file here
- oft.open("file.txt");
- if (oft.is_open()) {
- oft << "Tom Riddle had succeeded in creating the 6 offsprings of his misery and monstrosity." << endl << "I say offspring, as his soul was so broken, that you could say they were infernal children of his collective being." << endl << "He created his 6 intended horcruxes in this order..." << endl << "The first one he created took host in his diary, on 13 June, 1943. The chosen victim was the poor Myrtle Warren." << endl << "His second fragment, he placed on his mother's ring, in 29 May, 1993, using the death of his relative, Tom Riddle Senior" << endl << "The next three hosts are artifacts belonging to each of the houses of Hogwarts, Salazar Slytherin's amulet, in July 1996. Helga Hufflepuff's cup, in circa. 1946 or later, and lastly, Rowena Ravenclaw's diadem, circa 1946 or later. The 3 accompanying murders were namely: A Muggle tramp, Hepzibah Smith and lastly, an Albanian peasant." << endl << "And his last intended target was none other than his snake, Nagini. Made so by the murder of the tortured Bertha Jorkins, Summer 1994, in Albania7986" << endl;
- oft.close();
- }
- else {
- cout << "Error opening file" << endl;
- return -1;
- }
- ifstream ift;
- ofstream oft2;
- string a;
- ift.open("file.txt");//reading file
- if (ift.is_open()) {
- while (!ift.eof()) {
- ift >> a;
- for (int i = 0; i < a.length(); i++) { //Here the program is checking for numbers
- if (a[i] == '1' || a[i] == '2' || a[i] == '3' || a[i] == '4' || a[i] == '5' || a[i] == '6' || a[i] == '7' || a[i] == '8' || a[i] == '9') {
- a[i] = '*';
- }
- }
- int b = a.length(); //storing the length of the string every loop
- for (int j = b; j < b+1; j++) { //if the temporary variable j is equal to b, which it always will when the string is read, to the string will be added a space to separate them
- a = a + " ";
- }
- oft2.open("filea.txt"); //opening a new file
- if (oft2.is_open()) {
- oft2 << a; //storing variable a here very loop, but only the last string is pasted...
- oft2.close();
- }
- else {
- cout << "Error opening file." << endl;
- return -1;
- }
- }
- }
- else {
- cout << "Error opening file" << endl;
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement