Thread: ios::out question

  1. #1
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40

    ios::out question

    I need to figure out a way to move the cursor in "ios:: out" mode without overwriting stuff. The problem is that I need to move by the line, not by the character. Is there any way to do this? Thanks for any help.

    note: space in "ios:: out" to prevent smilie.

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    need more description.....

    are you refering to a console program (black screen in dos console) ???

    please provide more info and details

  3. #3
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    Yes, this is a console app (i know what it is lol). I need to write to a text file that already has text in it. I need to insert something into the middle of it pretty much, but without writing over anything. I'll just post code:

    Code:
    void putit(){
    
    	File.open("data.txt",ios:: out);
    	
    	if (File.is_open()){
    
    /*Right here, I need to go to a certain line of the text file without overwriting anything.*/
    		
    		File<<money<<"\n";
    		cout<<"Enter your name for the high scores: ";
    		cin>>name;
    		File<<name<<"\n";
    
    		int index=0;
    
    		while (index+1<(oflines-(b-4))){
    			File<<TempIn[index]<<"\n";
    			index++;
    		}
    //That loop inserts the end of the file previoulsy stored, it isn't important...
    	}
    	File.close();
    
    }
    - Grady (:

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you can write:

    Code:
    ofstream fout("file.txt", ios::ate);
    Including ios::ate causes a seek to the end of the file to occur when the file is opened. Although ios::ate causes a seek to the end-of-file, I/O operations can still occur anywhere within the file.

    Play around with that...........or you can do more searches on Google for (ios::ate)

    good luck..........

    .............
    matheo917

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    (i overlooked something)
    also when you want to have both -- input and output at the same time to the same file, you want to declare an object of an fstream, without setting flags like ios::in or ios:ut...

    so my previous line of code should be:

    Code:
    fstream blahblah("file_name.txt", ios::ate);

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    im not sure i totaly understand your question but how bout using seekp()

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Load the file contents into a container, search the container for the appropriate location, insert the new material into the container at the appropriate spot shifting contents of the container as appropriate, then rewrite back to the file.

  8. #8
    Registered User kiss_psycho's Avatar
    Join Date
    Feb 2003
    Posts
    49
    i think you want to advance by line not character.

    To advance by line, you could try creating an object of fstream, and access the function istream::getline from class istream. and if u use ios:: out, and want to advance by char, ostream::seekp would help.

    Happy Hunting...
    Definition of Programmer : A red-eyed mumbling mammal capable of conversing with inanimate objects.

    Happy Hunting...
    The Root

  9. #9
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    This is very funny. I just wrote exactly this last night. I did what was recommended a few posts above: read the whole file into a container, modify the exact line, and write back out. If you use standard algorithms, there is really not much to it. I just typed in what I remember, I have no idea if this actually compiles, but hopefully you get the idea.

    Code:
    #include <iostream>
    #include <fstream>
    #include <list>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    // You need a class called line that overloads the operator>> and
    // operator<<:
    
    class Line {
    public:
        std::string ln_;
    
        Line(string& in) { ln_ = in; }
        friend istream& operator>>(istream& s, Line& l)
        {
            return getline(s,l.ln);
         }
        friend ostream& operator<<(istream& s, Line& l)
        {
            return s<<l.ln<<endl;
        }
    };
    
    // For reading in the file, you can do something very neat with a  
    // standard algorithm and a functor:
    
    // define a functor somewhere that stores the line to look for,
    // and what to replace it with. Basically you return the input line
    // unless it matches what you are looking for, when you return
    // the other string, ie:
    
    class ModifyLine {
        string look_for;
        string relpace_with;
    public:
    
        ModifyLine(string& lf, string& rw)
        {
             look_for = lf;
             replace_with = rw;
        }
        void operator()(Line& l) const
        {
            if ( l.ln == look_for )
                return Line(replace_with);
            else return l;
         }
    
    };
    
    int main()
    {
        // code somewhere:
        ifstream fs;
        fs.open("filename");
    
        // a list of lines
        list<Line> lines;
        string lookfor; // you gotta fill this in 
        string replace_with; // you gotta fill this in
    
        // transform file input according to ModifyLine, and store it in
        // lst
        transform( istream_iterator<Line>(fs), istream_iterator<Line>(), back_inserter(lst), ModifyLine(lookfor, replace_with));
    
       // write it back out
       ofstream of;
       of.open("filename");
    
       // another standard alogrithm!
       copy( lst.begin(), lst.end(), ostream_iterator<Line>(of,"\n"));
    
       // done... 
       return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM