Need help with text reversing progam..

This is a discussion on Need help with text reversing progam.. within the C++ Programming forums, part of the General Programming Boards category; i'm trying to write a program that will take the contents of one file and completely reverse it and put ...

  1. #1
    ThunderCow
    Guest

    Need help with text reversing progam..

    i'm trying to write a program that will take the contents of one file and completely reverse it and put it into a new file... what i have currently takes each word and reverses it then puts it into a file... for example:

    input: the dog was big
    current program: eht god saw gib
    what i want: gib saw god eht

    i had tried to use getline before but i could get it to work..

    heres my code so far:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    void main()
    {
    char filenameOld[30];
    char filenameNew[30];
    string text;
    int i;
    cout << "What file do you want to backwardsify?\n";
    cin.getline(filenameOld, 30, '\n');
    
    cout << "What do you want the new file to be called?\n";
    cin.getline(filenameNew, 30, '\n');
    
    ifstream a_file(filenameOld);
                               
    ofstream b_file(filenameNew); 
    
    	do
    	{
    		a_file>>text;
    		for (i=text.length(); i>=0; i--)
    		{
    			b_file<<text[i];
    		}
    		b_file<<" ";
    
    		cout<<".";
    	}
    	while (a_file.eof()!=1);
    
    cout<<"DONE\n";
    
    a_file.close();
    b_file.close();    
     
    
    }
    any suggestions?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want to reverse the whole file this will do it -

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        string filenameOld;
        string filenameNew;
        string text;
        vector <string> strs;
        
        cout << "What file do you want to backwardsify?\n";
        getline(cin,filenameOld, '\n');
    
        cout << "What do you want the new file to be called?\n";
        getline(cin,filenameNew,'\n');
    
        ifstream a_file(filenameOld.c_str());                     
        ofstream b_file(filenameNew.c_str()); 
    
        if(a_file.fail() || b_file.fail())
        {
            cout << "Unable to open files\n";
            return 1;
        }
    
        do
        {
            getline(a_file,text,'\n');
            reverse(text.begin(),text.end());
            strs.push_back(text);
            cout<<".";
        }
        while (a_file.eof()!=1);
    
    
        reverse(strs.begin(),strs.end());
    
        for (int j =0;j<strs.size();j++)
            b_file << strs[j] << endl;
    
    
        cout<<"DONE\n";
    
        return 0;
    }
     

    but if you only want to reverse each line then you won't need to store the strings in a vector and could just ouput each line after it has been reversed in the initial loop.

  3. #3
    ThunderCow
    Guest

    Thumbs up

    that was very helpful, thanks!

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    You could also try reading file one char at a time into a char array adding a single null char at the end, effectively making the entire file a single string. Then use strrev() function on the string and write it a single char at a time to the second file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble drawing text xlib.
    By Qui in forum Linux Programming
    Replies: 1
    Last Post: 05-10-2006, 12:07 PM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  3. Text positioning and Text scrolling
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2004, 12:35 AM
  4. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 04:33 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM

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