fstream problem mit put()



  • Hi,

    Ich möchte ein char in meinem file ändern an einer bestimmten stelle.

    #include <fstream>
    
    using namespace std;
    
    int main()
    {
            ofstream out("lol.txt", ofstream::app);
    
            out.seekp(ofstream::beg);
    
            char set = 'A';
            int pos = 1;
    
            out.seekp(pos);
    
            out.put(set);
    
            out.close();
    }
    

    lol.txt

    HkLLO
    

    Ich möchte HkLLO in "HALLO" ändern

    Nach den programm start:

    lol.txt

    HkLLO
    A
    

    Könnt ihr mir weiter helfen?

    😕


  • Administrator

    std::ios_base::app führt dazu, das jeglicher Inhalt, welcher geschrieben wird, ans Ende angefügt wird. Was du brauchst, ist eine Anweisung, welche den Dateiinhalt nicht verändert. Dies macht std::ios_base::in . Kann man auch hier nachlesen:
    http://www.cplusplus.com/reference/iostream/ofstream/ofstream.html

    #include <fstream>
    
    int main()
    {
    	std::ofstream out("./lol.txt", std::ios_base::in);
    
    	out.seekp(1);
    	out.put('A');
    
    	return 0;
    }
    

    Grüssli



  • Super schnelle antwort!
    Funkt. Prima!

    So dann Danke 😉


Log in to reply