you have already written code similar to that. Open a new file for output and write to it exactly like you did when you changed all the characters in the file to upper case.

actually in the uppercase i'm writing the code to a new file...because i still don't know how to overwrite to the existing file instead....

i want in the 2 cases as well as the uppercase one to overwrite the existing file...can u tell me what to write instead of creating new files ?

You don't read very well do you. Re-read my post #45 because I explained how to do that. You can not write directly to the existing file but you have to write to a new file.

I suppose you could write to the existing file instead of a new by if you read the entire file into one big buffer or linked list of lines, change the data in memory, close the input file, open the file for output so that its contents are truncated, then finally write all the in-memory lines out to the file.

i've actually read it...and i searched for what u told me to do about the replace() and rename() but i couldn't figure out a clue...so if u would like maybe u can explain how can i operate them at least.

To delete a file -- remove(const char* filename);

string filename = "Hello.txt";
remove(filename.c_str());

rename a file

string original_filename = "temp.txt";
string new_filename = "Hello.txt";
rename( original_filename.c_str(), new_filename.c_str());

Now put those two code snippes together and you have the problem solved.

thnx man...u really did me an awesome favour...i've managed to mix these codes to overwrite on the original file concerning the uppercase-encrypt and decrypt cases...
but plz one last question and my program will be rockin,cuz this one i asked you about it before but u didn't reply....

in case 1,when i add new text to the end of the file...i told the user to start typing as much text as he wants.and when he finishes,he has to type (^z) to go back to the menu.....the problem is that afterwards when i read the file..i find everything he typed there but at the end with the command (^z)....i don't want (^z) to be typed as well in the file....how can i do this ??? look case 1 and tell me what's the solution plz.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
    int choice;
    int i=1;
    string myFile ;
    

    ifstream infile;
    ofstream outfile;
    cout << "Welcome To My Text Editor" <<endl;
    cout << "Please Enter Your File's Name: ";
    
    cin >> myFile;
    infile.open(myFile.c_str());
if (!infile) {
        cout << "The File "<< myFile.c_str() << " Couldn't Be Opened."<< endl;
        outfile.open (myFile.c_str());
        cout << "The File " << myFile.c_str() << " Doesn't Exist.Instead,I Created It For You" << endl;
        outfile.close();
             }
             
else { cout << "File Successfully Opened" << endl;
       while (infile.good())
       cout << (char) infile.get();
     }
     infile.close();
    while (i>0)
    {
              
          cout << "\n\n" << "1.  Add New Text To The End Of The File" << endl;
          cout << "2.  Display The Content Of The File" << endl;
          cout << "3.  Empty The File" << endl;
          cout << "4.  Encrypt The File Content" << endl;
          cout << "5.  Decrypt The File Content" << endl;
          cout << "6.  Merge Another File" << endl;
          cout << "7.  Count The Number Of Words In The File" << endl;
          cout << "8.  Search For A Word In The File" << endl;
          cout << "9.  Count The Number Of Times A Word Exists In The File" << endl;
          cout << "10. Turn The File Content To Upper Case" << endl;
          cout << "11. Exit" << endl;
    
          cout << "\nPlease Enter Your Choice From The Menu Above: " ;
          cin >> choice;
          cout << "\n" ;
          switch (choice)
          {
                 case 1: {
                          outfile.open (myFile.c_str(), ios::app);
                          cout << "....Type (^z) Whenever You Finish Typing....\n" << endl;                     
                          string word;
                          
                          while( word != "^z" )
                          {
                          cin >> word; 
                          outfile << " " << word ;
                          }
                          outfile.close();
                          }
                          continue;
                         
                           
                 case 2:                           
                          infile.open(myFile.c_str());
                          infile.clear();
                          while (infile.good())
                          cout << (char) infile.get();
                          infile.close();continue;
                         
                 
                 case 3:  
                          outfile.open (myFile.c_str(), ios::trunc);
                          cout << "File Cleared" << endl;
                          outfile.close();continue;
                          
                          
                 case 4:   {
                          infile.open(myFile.c_str());
                          outfile.open("new.txt");
                          char c;
                          infile.clear();
                                            
                          while((c = infile.get()) > 0 )
                         {
                          c = c + 1;          
                           outfile << c;                         
                          }
                          infile.close();
                          outfile.close();
                          string filename = "new.txt";
                          remove(myFile.c_str());
                          rename(filename.c_str(),myFile.c_str());   
                      cout << "File Encryption Completed" << endl;                             
                                                
                      continue; }   
                 
                 
                 case 5:  
                         {
                          infile.open(myFile.c_str());
                          outfile.open("new.txt");
                          char c;
                          infile.clear();
                                            
                          while((c = infile.get()) > 0 )
                         {
                          c = c - 1;          
                           outfile << c;                         
                         }
                          infile.close();
                          outfile.close();
                          string filename = "new.txt";
                          remove(myFile.c_str());
                          rename(filename.c_str(),myFile.c_str());
                          
                          cout << "File Decryption Completed" << endl;                             
                          
                          continue; }
                          
                          
                case 6:  {
                           outfile.open (myFile.c_str(), ios::app);
                           ifstream infile2 ("text.txt");
                           
                           outfile << ". " << infile2.rdbuf();
                           outfile.close();
                           infile2.close();
                           }
                          cout << "File Successfully Merged " << endl;
                          continue;
                          
                          
                 case 7:    
                          infile.open( myFile.c_str());
 
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          unsigned long wordCount = 0 ;

                          while( infile >> word )
                         {
                          wordCount++;

                         }

                          cout << "The File Contains " << wordCount << " Word(s) In It." << endl;
                          infile.close(); 
                         }
                          continue;
                          
                          
                 case 8:  infile.open( myFile.c_str());
                           
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          string wordtofind;
                          
                          cout << "Please Enter A Word To Search For:  " ;
                          cin >> wordtofind ;
                          while( infile >> word )
                         
                          if (word == wordtofind){
                          cout << "Word Exists In The File";break;}
                          
                         
                          if (word != wordtofind){
                          cout << "Word Doesn't Exist In The File";}
                          
                          infile.close(); 
                         }
                          continue;
                                   
                          
                 case 9:  infile.open( myFile.c_str());
                           
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          string wordtofind;
                          unsigned long wordCount = 0 ;
                          cout << "Please Enter A Word To Search For:  " ;
                          cin >> wordtofind ;
                          while( infile >> word )
                         {
                          if (word == wordtofind){
                          wordCount++;}

                         }

                          cout << "\nThe Word " << wordtofind << " Exists " << wordCount << " Time(s)." << endl;
                          infile.close(); 
                         }
                          continue;        
                                  
                 
                 case 10:   {
                            infile.open( myFile.c_str());
                            ofstream outfile("new.txt");
                            char c;
                            infile.clear();
                            
                            
                            while( (c = infile.get()) > 0 )
                            
                            outfile << (char)toupper(c);
                            infile.close();
                            outfile.close();
                            string filename = "new.txt";
                            remove(myFile.c_str());
                            rename(filename.c_str(),myFile.c_str());
                            
                            cout << "File Successfully Converted";
                            
                            
                            }
                            continue; 
                                            
                      
                 case 11: cout << "Thank You For Using My Text Editor" << endl; break;
                 
                 
                 default: cout << "Choice Not Available,Please Choose An Appropriate Choice"; continue;
                                        
                 
                 }
                     
                 system ("Pause");
                 return 0;
                 }
                 
  }

test for ^Z before writing to the file

if( word != "^Z")
   cout << " " << word;

Also not that "^Z" (which is 2 characters) is not the same as '^Z' (which is only 1 character that you generate by hitting the Ctl key plus Z key at the same time, or Ctrl+Z. And that is the usual way to terminate an input other than by the <Enter> key.

i did it like that but it didn't work yet...

case 1: {
                          outfile.open (myFile.c_str(), ios::app);
                          cout << "....Press (CTRL + Z) Whenever You Finish Typing....\n" << endl;                     
                          string word;
                          
                          while( word =="^Z")
                          {
                          cin >> word;
                          outfile << " " << word ;
                          }
                          outfile.close();
                          }
                          continue;

sry i posted something wrong in the last code...here it is....it now works and doesn't show the ^Z character in the file finally...
but instead of typing the character (^Z).i want the user to finish typing by pressing CTRL+Z...
i tried to make it like this '^Z' instead of "^Z" but it didn't work...here's the code now..
tell me just the final touch on what to add to make the user finish by press ctrl+z..

case 1: {
                          outfile.open (myFile.c_str(), ios::app);
                          cout << "....Type (^Z) Whenever You Finish Typing....\n" << endl;                     
                          string word;
                          
                          while( word != "^Z")
                          {
                          cin >> word;
                          if(word == "^Z")
                          {
                          break;
                          }
                          outfile << " " << word;
                          }
                          
                          
                          outfile.close();
                          }
                          continue;

You cannot compare string like that use functions like compare ,etc.

all i wanna do is for the user to press CTRL+z to return to the menu rather than typing ^Z.....how can i do this in case 1 above ?

After some testing I come to the conclusion that it is not possible to use Ctrl+Z as you intend because the os gobbles it up and won't put it in the output string. But you can use some other Ctrl+? key such as Ctrl+A In the code below function getword() returns TRUE if the Ctrl+A key was hit during the input string or false if not.

#include <string>
#include <iostream>
using namespace std;
const int endofinput = 0x01;

bool getword(string & word)
{
    char c;
    bool ctrlZ = false;
    do {
        cin.read(&c, 1);
        if( c != endofinput)
            word += c;
        else
            ctrlZ = true;
    } while( c != endofinput && !isspace(c));
    return ctrlZ ;
}
int main(void)
{
    string word = "";
    while( getword(word) != endofinput)
    {
            cout << " " << word;
    }
    
  return 0;
}

i read what u wrote and i understood what u meant....but meanwhile.
i tested also somethings on case 1 and i concluded a working one with the ctrl+z key which finishes typing as i want...but the problem is that it works when i add (break;)at then of the code.but it doesn't for sure return to the menu again...and when i replace break with continue,it goes into an endless loop....here's the code for case 1 and see if you can figure out a solution to let it return back to the menu rather than closing the program..

case 1: {
                          outfile.open (myFile.c_str(), ios::app);
                          string text;
                          cout << "Enter Text (or CTRL z to end): " << endl;
                          
                          

                          while (! cin.fail())
                          {
                           
                            outfile << text ;
   
                             cin >> text;
                            }
                            outfile.close();
                            break;
                            }

break jumps out of the loop but continue just jumps to the top of the loop and starts again. Before the break you need to clear the fail flag. cin.clear()

thnx man for your efforts....finally i'm glad to say that my program is now finished and working like a charm...and i have really learned a lot from this program about strings and files...

anyway...here's my final working code...and thnx again for helping me.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
    int choice;
    int i=1;
    string myFile ;
    

    ifstream infile;
    ofstream outfile;
    cout << "Welcome To My Text Editor" <<endl;
    cout << "Please Enter Your File's Name: ";
    
    cin >> myFile;
    infile.open(myFile.c_str());
if (!infile) {
        cout << "The File "<< myFile.c_str() << " Couldn't Be Opened."<< endl;
        outfile.open (myFile.c_str());
        cout << "The File " << myFile.c_str() << " Doesn't Exist.Instead,I Created It For You" << endl;
        outfile.close();
             }
             
else { cout << "File Successfully Opened" << endl;
       while (infile.good())
       cout << (char) infile.get();
     }
     infile.close();
    while (i>0)
    {
              
          cout << "\n\n" << "1.  Add New Text To The End Of The File" << endl;
          cout << "2.  Display The Content Of The File" << endl;
          cout << "3.  Empty The File" << endl;
          cout << "4.  Encrypt The File Content" << endl;
          cout << "5.  Decrypt The File Content" << endl;
          cout << "6.  Merge Another File" << endl;
          cout << "7.  Count The Number Of Words In The File" << endl;
          cout << "8.  Search For A Word In The File" << endl;
          cout << "9.  Count The Number Of Times A Word Exists In The File" << endl;
          cout << "10. Turn The File Content To Upper Case" << endl;
          cout << "11. Exit" << endl;
    
          cout << "\nPlease Enter Your Choice From The Menu Above: " ;
          cin >> choice;
          cout << "\n" ;
          switch (choice)
          {
                 case 1: {
                          outfile.open (myFile.c_str(), ios::app);
                          string text;
                          cout << "Enter Text (Or CTRL+Z To End): " << endl;
                                                   

                          while (! cin.fail())
                          {
                           
                            outfile << " " << text ;
   
                             cin >> text;
                            }
                            outfile.close();
                            cin.clear();
                            continue;
                            }
                                                    
                           
                        
                          
                 case 2:                           
                          infile.open(myFile.c_str());
                          infile.clear();
                          while (infile.good())
                          cout << (char) infile.get();
                          infile.close();continue;
                         
                 
                 case 3:  
                          outfile.open (myFile.c_str(), ios::trunc);
                          cout << "File Cleared" << endl;
                          outfile.close();continue;
                          
                          
                 case 4:   {
                          infile.open(myFile.c_str());
                          outfile.open("new.txt");
                          char c;
                          infile.clear();
                                            
                          while((c = infile.get()) > 0 )
                         {
                          c = c + 1;          
                           outfile << c;                         
                          }
                          infile.close();
                          outfile.close();
                          string filename = "new.txt";
                          remove(myFile.c_str());
                          rename(filename.c_str(),myFile.c_str());   
                      cout << "File Encryption Completed" << endl;                             
                                                
                      continue; }   
                 
                 
                 case 5:  
                         {
                          infile.open(myFile.c_str());
                          outfile.open("new.txt");
                          char c;
                          infile.clear();
                                            
                          while((c = infile.get()) > 0 )
                         {
                          c = c - 1;          
                           outfile << c;                         
                         }
                          infile.close();
                          outfile.close();
                          string filename = "new.txt";
                          remove(myFile.c_str());
                          rename(filename.c_str(),myFile.c_str());
                          
                          cout << "File Decryption Completed" << endl;                             
                          
                          continue; }
                          
                          
                case 6:  {
                           outfile.open (myFile.c_str(), ios::app);
                           ifstream infile2 ("text.txt");
                           
                           outfile << ". " << infile2.rdbuf();
                           outfile.close();
                           infile2.close();
                           }
                          cout << "File Successfully Merged " << endl;
                          continue;
                          
                          
                 case 7:    
                          infile.open( myFile.c_str());
 
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          unsigned long wordCount = 0 ;

                          while( infile >> word )
                         {
                          wordCount++;

                         }

                          cout << "The File Contains " << wordCount << " Word(s) In It." << endl;
                          infile.close(); 
                         }
                          continue;
                          
                          
                 case 8:  infile.open( myFile.c_str());
                           
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          string wordtofind;
                          
                          cout << "Please Enter A Word To Search For:  " ;
                          cin >> wordtofind ;
                          while( infile >> word )
                         
                          if (word == wordtofind){
                          cout << "Word Exists In The File";break;}
                          
                         
                          if (word != wordtofind){
                          cout << "Word Doesn't Exist In The File";}
                          
                          infile.close(); 
                         }
                          continue;
                                   
                          
                 case 9:  infile.open( myFile.c_str());
                           
                          infile.clear();
                          if( infile.is_open() )
                         {
                          string word;
                          string wordtofind;
                          unsigned long wordCount = 0 ;
                          cout << "Please Enter A Word To Search For:  " ;
                          cin >> wordtofind ;
                          while( infile >> word )
                         {
                          if (word == wordtofind){
                          wordCount++;}

                         }

                          cout << "\nThe Word " << wordtofind << " Exists " << wordCount << " Time(s)." << endl;
                          infile.close(); 
                         }
                          continue;        
                                  
                 
                 case 10:   {
                            infile.open( myFile.c_str());
                            ofstream outfile("new.txt");
                            char c;
                            infile.clear();
                            
                            
                            while( (c = infile.get()) > 0 )
                            
                            outfile << (char)toupper(c);
                            infile.close();
                            outfile.close();
                            string filename = "new.txt";
                            remove(myFile.c_str());
                            rename(filename.c_str(),myFile.c_str());
                            
                            cout << "File Successfully Converted";
                            
                            
                            }
                            continue; 
                                            
                      
                 case 11: cout << "Thank You For Using My Text Editor" << endl; break;
                 
                 
                 default: cout << "Choice Not Available,Please Choose An Appropriate Choice"; continue;
                                        
                 
                 }
                     
                 system ("Pause");
                 return 0;
                 }
                 
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.