Announcement

Collapse
No announcement yet.

formatierte ausgabe in eine datei

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • formatierte ausgabe in eine datei

    hallo,

    ich würde gerne daten (x und y) spaltenweise formatiert in eine datei schreiben. das habe ich getan:
    ofstream interpolation;
    interpolation.open("interpol_neu.dat");
    for(int ii=0; ii<50; ii++){
    interpolation << ("%15.4lf %15.4lf\n", axneu[ii], ayneu[ii]) << endl;
    }
    interpolation.close();

    kommt was komisches raus.
    sollte so sein:

    0.0000 12.0000
    1.1234 3.4456
    usw.

    wie bekomme ich das hin bitte?

    danke

  • #2
    Formatierung

    Hallo rasidrasid!

    Hier hast du zwei Beispiele für Formatierungen:

    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <iomanip>
    using namespace std;


    int _tmain(int argc, _TCHAR* argv[])
    {
    string x;
    stringstream ss;
    double dZahl = 123.456;

    ss.setf(ios::fixed);
    ss.setf(ios::showpoint);
    ss.precision(2);

    ss << "Formatierte Ausgabe:" << dZahl << endl;

    getline(ss, x); //----> Das x kannst du dann in eine Datei schieben.

    cout << "--> " << x << ">" << endl;

    return 0;
    }

    Schau dir die Erklärung von stringstream an. Die Standard-Ausgabe ist ähnlich aufgebaut. Bsp.:

    #include <iostream>
    #include <cstdio>
    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
    int i;

    // 1) Dezimal/Hexadezimal/Oktal
    // ============================
    cout.setf(ios::showbase | ios::uppercase | ios::dec);
    for (i=0; i<=15; i++) { cout.width(4); cout << i; }
    cout << endl;
    cout.unsetf(ios::dec); cout.setf(ios::hex);
    for (i=0; i<=15; i++) { cout.width(4); cout << i; }
    cout << endl;
    cout.unsetf(ios::hex); cout.setf(ios:ct);
    for (i=0; i<=15; i++) { cout.width(4); cout << i; }
    cout << endl << endl;

    // 2) Ausrichtung und Fuellzeichen
    // ===============================
    cout.unsetf(ios::hex); cout.setf(ios::dec);
    cout.setf(ios::left); cout.fill('.'); cout.width(10);
    cout << 1998 << endl;
    cout.setf(ios.right); cout.fill('.'); cout.width(10);
    cout << 1998 << endl << endl;

    // 3) Ausgabe von Gleitkommazahlen
    // ===============================
    double x = 12.34, y = 5678.9123456, z = 1e5;

    printf("Festpunkt : (%12.2lf,%12.2lf,%12.2lf)\n", x, y, z);

    cout.fill(' '); cout.setf(ios::fixed); cout.precision(2);
    cout << "Festpunkt : (";
    cout.width(12); cout << x << ',';
    cout.width(12); cout << y << ',';
    cout.width(12); cout << z << ')' << endl;

    printf("Gleitpunkt : (%12.2le,%12.2le,%12.2le)\n", x, y, z);

    cout.unsetf(ios::floatfield | ios::uppercase);
    cout.fill(' '); cout.setf(ios::scientific); cout.precision(2);
    cout << "Gleitpunkt : (";
    cout.width(12); cout << x << ',';
    cout.width(12); cout << y << ',';
    cout.width(12); cout << z << ')' << endl;
    }

    Gruß
    Ersin.

    Comment

    Working...
    X