Need to invert the output!

This is a discussion on Need to invert the output! within the C++ Programming forums, part of the General Programming Boards category; hello ppl... ok i output this string to a .txt file using ifstream... Code: ofstream fout; //to write out to ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Exclamation Need to invert the output!

    hello ppl...

    ok i output this string to a .txt file using ifstream...

    Code:
    ofstream fout; //to write out to an output file
    fout.open("outputfile.txt");
    
    do 
    {
       fout << something;
    }
    while(something)
    
    fout << flush;
    fout.close();
    now suppose i was printing city names as output...if it comes in the order Adelaide, Canberra, Melbourne, Sydney during the print to the file, I want it to print in the other direction...

    my output should be: Sydney, Melbourne, Canberra, Adelaide...


    I guess there's some string copy and then invert operation but i thought you could something like this directly while printing...

    any ideas?

    Regards,

    Farooq

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    How are you storing your list of cities that you wish to output? Is this an array or some container of strings? It can be as easy as simply looping in a reverse direction as you perform the output steps.
    "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

  3. #3
    Karsten
    Join Date
    Sep 2004
    Posts
    8
    since iam still a newbie to C/C++,i would suggest you try to make a function ( class ) in this fifo ( first in first out ) style ,this way you always have the one you need and in the direction you want it

    Hoschi

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    If you are using a map as in your previous thread, you need only use a reverse iterator to loop in the reverse direction like hk_mp5kpdw suggested.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ok here's the code...I'm using a heap for these nodes...Jlou u're right i used the MAP and it works like a charm...but that's only to store all the nodes when reading in the file...this is the write to file code:

    Code:
    ofstream fout; //to write out to an output file
                    fout.open("outputfile.txt");
    
    		if( currentState == SUCCEEDED )
    		{
    			//Iterate back to the startNode from the endNode
    			Node *nodeChild = endNode;
    			Node *nodeParent = endNode->parent;
    
    			do
    			{     
                                fout << nodeChild->CityName << " ";
                                distance = distance + nodeChild -> getDistanceEstimate((nodeParent)-> x, (nodeParent)-> y);
    	                    nodeChild = nodeParent;
    	                    nodeParent = nodeParent->parent;
    
    			}
    			while( nodeChild != startNode );
    
                       fout << distance << " ";
                       fout << nodeChild->CityName << " "; 
    		}
    
                    else
                    {
                       fout << "No solution ";
                    }
    
                    fout << diff << " sec" << "\n";
                    fout << flush;
                    fout.close();
    
    		return;
    	}
    Regards,

    Farooq
    Last edited by alvifarooq; 09-21-2004 at 09:06 PM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ok if you don't get what i mean from the code...this is what i'm doing...

    i store the parent of each node and then when my search is over i backtrack...so i go from endnode to startnode...

    now while i go from endnode to startnode at each step i print out the Cityname in each node...this way the names of cities on the file are from the goal city to the start city whereas i want them to be output in the other direction...

    i could technically save them in a string and then do the reverse looping thing...but this is an additional step and i don't want to limit the efficiency of the code...

    i could also declare a childnode pointer and then instead of going backwards...go forwards...but i was hoping there's a better way like when outputting using ifstream i could tell it to add elements to the text file from the start and then keep on pushing things at the start rather than appending at the end...

    Regards,

    Farooq

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Maybe push the nodes into a deque as you are performing the search from start node to end node, if a solution is found then pop them off the front and print them out. If you have to back track in your searching algorithm then you pop off the node stored at the back.
    "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

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    ok all else fails so i've declared a vector of strings...i use:
    Code:
    vector<string> PathOutput;
    then:
    Code:
    PathOutput.push_back(nodeChild->CityName);
    to store the names in the string. Then I iterate using:

    Code:
    for (vector<string>::iterator i = PathOutput.end(); i != PathOutput.begin(); i--)
                       {
                          fout << *i << " ";
                       }
    well with this nothing prints out...although if i use the iterator in the forward direction i get the same results as before...am i usng the reverse iterator wrongly?

    Regards,

    Farooq
    Last edited by alvifarooq; 09-23-2004 at 07:12 AM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    thanks guys...turns out i am using the reverse iterator in a wrong way:

    Code:
    for (vector<string>::reverse_iterator i = PathOutput.rbegin(); i != PathOutput.rend(); i++)
                       {
                          fout << *i << " ";
                       }
    works just fine now...thanks a lot...though if anyone can come up with a way to do this without saving in a vector first...ill be grateful...

    thanks

    Farooq

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    There are many ways you can do this:

    Code:
    for (vector<string>::reverse_iterator i = PathOutput.rbegin(); i != PathOutput.rend()(); ++i)
    {
        fout << *i << " ";
    }
    Or use the reverse_copy function:

    Code:
    #include <algorithm>
    #include <iterator>
    ...
    std::reverse_copy(PathOutput.begin(),PathOutput.end(),std::ostream_iterator<std::string>(fout," "));
    Or use the forward copy function but with reverse iterators instead:

    Code:
    #include <algorithm>
    #include <iterator>
    ...
    std::copy(PathOutput.rbegin(),PathOutput.rend(),std::ostream_iterator<std::string>(fout," "));
    Or you could reverse the order of the elements in the vector first and then use a traditional begin->end output loop:

    Code:
    #include <algorithm>
    ...
    std::reverse(PathOutput.begin(),PathOutput.end());
    for( vector<string>::iterator i = PathOutput.begin(); i != PathOutput.end(); ++i )
    {
        fout << *i << " ";
    }
    Etc, etc, etc... I can go on and on and on...
    "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

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    this will be helpful to me later on...thanks a lot...

    Regards,

    Farooq

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If the file is completely predictable, as in a file of database fields, then you could use stream methods to manipulate the file pointer such that you read the last set of data first and then read data "backward". Short of that, you will need to read the information into a container in your program and manipulate it from there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

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