Inhalt von 2D-Array in File (.txt) speichern



  • Hallo

    Wie kann man am einfachsten den Inhalt eines Arrays (float values) in einem txt-File abspeichern? Danke im Voraus für Deine Hilfe.
    houver



  • Ungetestet, etwa so:

    include <fstream.h>
    
    void main()
    {
      using namespace std;
    
      AnsiString fname="xyz.txt"; 
      int myarray[XMAX][YMAX];
    
      ofstream oFile(fname.c_str(), ios::out);
    
      for (int iy=0;iy<XMAX;iy++)
      {
        for (int ix=0;ix<YMAX;ix++)      
        {
          oFile<<myarray[ix][iy]<<endl;
        }
      }
      oFile.close();
    }
    

    Allerdings könntest du ruhig vorher mal etwas Gehirnschmalz investieren und deinen Vorschlag dann posten. Die Suchfunktion & FAQ helfen dir zu dem Thema auch.

    mfG

    robi1806



  • ich hab folgendes getestet: matrix hat row zeilen und col spalten

    ofstream myfile ("my.out");

    if (myfile.is_open() )
    {
    for (int i=0; i<row; i++)
    {
    for (int j=0; j<col; j++)
    {
    myfile << matrix[i][j] << "\t" ;
    }
    myfile << endl;
    }
    myfile.close();
    }

    man darf nicht vergessen ein endl einzubauen nach jeder zeile. robi hat das oben nach jedem elemnt, d.h. eine n x m matrix wird immer als n*m x 1 vector geschrieben.
    duschl



  • <delete>...</delete>
    ooops, falsches Posting.


Anmelden zum Antworten