Abzeichen Archivirungs Programm 2 Probleme



  • Ich brauche eure hilfe.

    1. Problem
    ich will in einem Programm einen Text mit Leerzeichen und Zahlen in eine Datei z.B. test.txt speichern. Das Problem ist ich kann nur einen zusammenhängenden Text ohne Leerzeichen in die datei speichern. ich habe es mit char text[256]; und string text; ausprobiert, das Programm hängt sich auf wenn ich text mit leerzeichen eingebe.

    2. Problem
    Wenn ich in die bereits angefangene Datei einen text hinzufügen möchte wird der ganze text gelöscht und durch den neuen(der eigendlich nur hinzugefügt werden sollte) ersetzt.

    hier der code

    ofstream outFile;
    
    string abzna;
    string abzlo;
    
    cout<<"Name : ";
    cin>>abzna;
    clrscr();
    cout<<"Lagerort : ";
    cin>>abzlo;
    
    {
    outFile.open("abz.sven");
    outFile<<"Name:                 Lagerort:   "<<endl;
    outFile<<endl;
    
    outFile<<abzna<<"                 ";
    outFile<<abzlo<<endl;
    outFile<<endl;
    }
    
    getch();
     outFile.close();
    


  • Ach noch was. Das Programm soll auch die Dateien auslesen können und in der Konsole anzeigen.



  • Sorry fürs spamming...
    Ich code unter Dev c++ neuste Version



  • 1. Hängt es sich schon bei der Eingabe auf? Oder erst beim Abspeichern in der Datei?

    2. Falscher Open-Modus. Sieh dir dazu folgendes an:

    An integer that contains mode bits defined as ios enumerators that can be combined with the bitwise OR ( | ) operator. The nMode parameter must have one of the following values:

    ios::app The function performs a seek to the end of file. When new bytes are written to the file, they are always appended to the end, even if the position is moved with the ostream::seekp function.

    ios::ate The function performs a seek to the end of file. When the first new byte is written to the file, it is appended to the end, but when subsequent bytes are written, they are written to the current position.

    ios::in If this mode is specified, then the original file (if it exists) will not be truncated.

    ios::out The file is opened for output (implied for all ofstream objects).

    ios::trunc If the file already exists, its contents are discarded. This mode is implied if ios::out is specified and ios::ate, ios::app, and ios:in are not specified.

    ios::nocreate If the file does not already exist, the function fails.

    ios::noreplace If the file already exists, the function fails.

    ios::binary Opens the file in binary mode (the default is text mode).

    Du benötigst in diesem Fall ios::app ->

    outFile.open ( "abz.sven" , ios::out | ios::app );
    

    Dieser Thread sieht mir sehr nach Std-C++ aus. Ich verschiebe ihn mal dorthin!

    MfG SideWinder



  • Achja Standardopenmodus ist: ios::out | ios::trunc.

    MfG SideWinder



  • Nach dem ich den Text z.B. Hallo Coder eingegeben und RETURN gedrückt habe. aber nur bei einem Text mit leerschritten

    gruß sveb



  • Kann es sein, dass das Programm nicht abstürtzt, sondern eher frühzeitig beendet?

    IMHO liest cin strings nur bis zum Leerzeichen.
    D.h. wenn du nen Text mit einem Leerzeichen eingibst, dann werden gleich beide strings damit gefüttert, der erste mit dem Wort bis zum Leerzeichen, der andere mit dem was folgt.

    Wenn du die gesamte eingegebene Zeile im string haben willst, schau dir mal cin.getline(puffer, länge) an.


Anmelden zum Antworten