Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Write a matrix to a txt file

Write a matrix to a txt file

Scheduled Pinned Locked Moved C / C++ / MFC
ios
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dairiseky
    wrote on last edited by
    #1

    I want to insert the values i get from a matrix, in the order thay are in a text file. I mean by this the values to keep their place, on lines and columns. To read the values I did this: double LocalMap::SquareValue(int i, int j) { for (i=0; i0){ cellsV[i][j]=cellV[i][j]*cellV[i][j]; if (cellsV[i][j]>noise_border){ ofstream BuiltMap; //mode écriture BuiltMap.open ("C:\\My Files\\BuiltMap\\BuiltMap.txt", ios::in); //open file while (!BuiltMap.eof()){ //laisser i colonnes libres et j espaces libres BuiltMap<<"\n"<<" "<

    D 1 Reply Last reply
    0
    • D dairiseky

      I want to insert the values i get from a matrix, in the order thay are in a text file. I mean by this the values to keep their place, on lines and columns. To read the values I did this: double LocalMap::SquareValue(int i, int j) { for (i=0; i0){ cellsV[i][j]=cellV[i][j]*cellV[i][j]; if (cellsV[i][j]>noise_border){ ofstream BuiltMap; //mode écriture BuiltMap.open ("C:\\My Files\\BuiltMap\\BuiltMap.txt", ios::in); //open file while (!BuiltMap.eof()){ //laisser i colonnes libres et j espaces libres BuiltMap<<"\n"<<" "<

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      for (i = 0; i < nx; i++)
      {
      for (j = 0; j < ny; j++)
      {
      if (cellV[i][j] > 0)
      {
      cellsV[i][j] = cellV[i][j] * cellV[i][j];
      if (cellsV[i][j] > noise_border)
      {
      ofstream BuiltMap;
      BuiltMap.open("C:\\My Files\\BuiltMap\\BuiltMap.txt", ios::in);
      while (! BuiltMap.eof())
      {
      BuiltMap << "\n" << " " << cellsV[i][j];
      }
      }
      else
      cellsV[i][j] = NULL;
      }
      }
      }

      Aside from the fact that the opening/closing of the file should be done outside of the loops, I'm not quite sure what you are doing here. You've got an ofstream object that is opening a file for input (i.e., reading), yet you are usign the << operator to write to it. Is this intentional?


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      D 1 Reply Last reply
      0
      • D David Crow

        for (i = 0; i < nx; i++)
        {
        for (j = 0; j < ny; j++)
        {
        if (cellV[i][j] > 0)
        {
        cellsV[i][j] = cellV[i][j] * cellV[i][j];
        if (cellsV[i][j] > noise_border)
        {
        ofstream BuiltMap;
        BuiltMap.open("C:\\My Files\\BuiltMap\\BuiltMap.txt", ios::in);
        while (! BuiltMap.eof())
        {
        BuiltMap << "\n" << " " << cellsV[i][j];
        }
        }
        else
        cellsV[i][j] = NULL;
        }
        }
        }

        Aside from the fact that the opening/closing of the file should be done outside of the loops, I'm not quite sure what you are doing here. You've got an ofstream object that is opening a file for input (i.e., reading), yet you are usign the << operator to write to it. Is this intentional?


        "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

        D Offline
        D Offline
        dairiseky
        wrote on last edited by
        #3

        I thought ifstream is made for reading files only, and ofstream to be able to write into file. Did I miss something there? I want to write the values cellsV[i][j] into the BuiltMap file. THX

        D 1 Reply Last reply
        0
        • D dairiseky

          I thought ifstream is made for reading files only, and ofstream to be able to write into file. Did I miss something there? I want to write the values cellsV[i][j] into the BuiltMap file. THX

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          So shouldn't you be opening the file with ios::out instead?


          "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups