putback() not putting back?

This is a discussion on putback() not putting back? within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to run a program that will find the word "Son" in a text file and change ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    putback() not putting back?

    Hi I am trying to run a program that will find the word "Son" in a text file and change it into the word "Gun, but apparently it is not working because the function putback() doesn't seem to be putting back the character back into input stream.

    Code:
    /* This program will attempt to transfer an original file, find all references of a particular word, edit the word
       and write to a new file */
    
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    
    using namespace std;
    
    void editword(ifstream&input, ofstream&output);
    
    int main()
    {
    
     char outname[21];
     ofstream output;
     ifstream input;
    
     input.open("anyfile.txt");//suppose that undecided has already existr
    
     if ( input.fail() )
     {
      cout<<"The input file has failed to open!";
      exit(1);
     }
    
     cout<<"Please enter a name for the output file:"<<endl;
     cin>>outname;
    
     output.open(outname);
    
     if ( output.fail() )
     {
      cout<<"The input file has failed to open!";
      exit(1);
     }
    
     editword(input, output);
    
     cout<<"Editing has been completed!"<<endl;
    
    
     input.close();
     output.close();
    
     return 0;
    }
    
    void editword(ifstream&input, ofstream&output)
    {
      char find;
    
      input.get(find);
    
      while(!input.eof())
     {
    
    
      if(find=='S')
      {
         input.get(find);
         {
           if(find=='o')
          {
    
           input.get(find);
    
               {
                if(find=='n')
                {
                 output<<"Gun";
                }
                else
                {
                 input.putback(find);
                }
               }
         }
           else
         {
           input.putback(find);
           output.put(find);
         }
        }
      }
      else
      {
       output.put(find);
      }
    
    
      input.get(find);
     }
    }
    Could you find the problem? Thanks in advance!
    Last edited by Hybodus; 09-01-2011 at 06:48 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So if you have something like "So if you have something like" in your file, you will read the capital S, you will read the o, you will read the space, you will put back the space but not the S and the o. Presumably in that if -> if -> else case, you should write out "So" and then do the putback of the space character.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    while(!input.eof())
    Read: Why it's bad to use feof() to control a loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Putback() help
    By Ducky in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2007, 10:46 PM
  2. Ripping websites apart and putting them back together...
    By KONI in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 04-25-2007, 04:32 PM
  3. cin.peek() and cin.putback()
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 01-02-2006, 03:50 PM
  4. forcing window repainting after putting back caption
    By underthesun in forum Windows Programming
    Replies: 2
    Last Post: 02-05-2005, 09:13 PM
  5. Some woman back ended my car today, and back hurts
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 08-20-2003, 12:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21