Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. /*
  2. Write   a   program to  read    a   file    called  file.txt,   which   contains    an  arbitrary  
  3. number  of  characters. You have    to  create  a   new file    named   filea.txt   that    have   
  4. all the digits  in  file.txt    replaced    with    the symbol  *. 
  5. The text    file    can contain:    This file contains some random numbers like 1 and 3.
  6. And can have other numbers like 4566 and 563454. A  sample  filea.txt,  created by  the program,
  7. for the above   input,  is  shown   below:  This file contains some random numbers like * and *.
  8. And can have other numbers like **** and ******.
  9. */
  10.  
  11. #include <fstream>
  12. #include <iostream>
  13. #include <string>
  14.  
  15. using namespace std;
  16.  
  17. int main() {
  18.  
  19.     ofstream oft; //creating file here
  20.     oft.open("file.txt");
  21.     if (oft.is_open()) {
  22.  
  23.         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;
  24.  
  25.         oft.close();
  26.     }
  27.     else {
  28.         cout << "Error opening file" << endl;
  29.         return -1;
  30.     }
  31.  
  32.     ifstream ift;
  33.     ofstream oft2;
  34.    
  35.     string a;
  36.    
  37.     ift.open("file.txt");//reading file
  38.  
  39.     if (ift.is_open()) {
  40.         while (!ift.eof()) {
  41.             ift >> a;
  42.            
  43.             for (int i = 0; i < a.length(); i++) { //Here the program is checking for numbers
  44.                 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') {
  45.                     a[i] = '*';
  46.                 }
  47.             }
  48.  
  49.             int b = a.length(); //storing the length of the string every loop
  50.  
  51.             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
  52.                 a = a + " ";
  53.             }
  54.            
  55.             oft2.open("filea.txt"); //opening a new file
  56.             if (oft2.is_open()) {
  57.                 oft2 << a; //storing variable a here very loop, but only the last string is pasted...
  58.                 oft2.close();
  59.             }
  60.             else {
  61.                 cout << "Error opening file." << endl;
  62.                 return -1;
  63.             }
  64.            
  65.         }
  66.     }
  67.     else {
  68.         cout << "Error opening file" << endl;
  69.         return -1;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement