C++ I/O Readable

Sid Kraft 46 Reputation points
2026-07-13T00:10:00.6833333+00:00

Not sure what I am doing wrong but this should output readable values, does not:

#include fstream;

float PLOTX[20];

float PLOTY[20];

float PLOTZ[20];

int INOW=20;

I = 0;

//Open the output file for writing

ofstream out("C:/Plot Data");

do

{

 cout << PLOTX[I] << ' ' << PLOTY[I] << ' ' << PLOTZ[I];

 ++I;

} while (I <= INOW);

//Close the file

out.close();

When I review "c/Plot File" is empty?

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Nancy Vo (WICLOUD CORPORATION) 7,250 Reputation points Microsoft External Staff Moderator
    2026-07-13T04:43:12.7166667+00:00

    Hello @Sid Kraft ,

    Thanks for your question.

    Your file is empty because:

    • Use out instead of cout to write to the file.
    • Change I <= INOW to I < INOW to avoid accessing element 20 in a 20-element array.
    • There are some minor syntax mistakes::
      • #include <fstream>
      • int I = 0;

    You can refer to the following code example:

    #include <iostream>
    #include <fstream>
    
    int main() {
        float PLOTX[20] = {0};
        float PLOTY[20] = {0};
        float PLOTZ[20] = {0};
    
        int INOW = 20;
        int I = 0;
    
        std::ofstream out("C:/PlotData.txt");
    
        do {
            out << PLOTX[I] << ' ' << PLOTY[I] << ' ' << PLOTZ[I] << '\n';
            ++I;
        } while (I < INOW);
    
        out.close();
        return 0;
    }
    

    I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback. Thank you.

    Was this answer helpful?

    0 comments No comments

  2. AI answer

    2026-07-13T00:10:14.7866667+00:00

    The file is empty because the code never writes anything to the ofstream and the loop bounds are off.

    Key issues:

    1. No output to the file stream
      The loop writes only to cout:
         cout << PLOTX[I] << ' ' << PLOTY[I] << ' ' << PLOTZ[I];
      
      but never to out. To write to the file, use out:
         out << PLOTX[I] << ' ' << PLOTY[I] << ' ' << PLOTZ[I] << '\n';
      
    2. Incorrect include and file name
      • The include should be:
             #include <fstream>
        
        not #include fstream;.
      • The file path should include an extension and use consistent slashes, for example:
             ofstream out("C:/Plot Data.txt");
        
    3. Loop goes out of bounds
      float PLOTX[20]; defines indices 0 to 19.
      int INOW = 20; and
         I = 0;
         do {
             ...
             ++I;
         } while (I <= INOW);
      
      will access PLOTX[20] on the last iteration, which is out of range and undefined behavior. Use < and match the array size:
         int INOW = 20;   // number of valid points
         int I = 0;
         while (I < INOW) {
             out << PLOTX[I] << ' ' << PLOTY[I] << ' ' << PLOTZ[I] << '\n';
             ++I;
         }
      
    4. Optional: check for file open errors
      Before writing, verify the stream is good:
         ofstream out("C:/Plot Data.txt");
         if (out.fail()) {
             // handle error: path invalid, no permission, etc.
             return 1;
         }
      

    Putting it together:

    #include <fstream>
    #include <iostream>
    using namespace std;
    
    float PLOTX[20];
    float PLOTY[20];
    float PLOTZ[20];
    
    int main() {
        int INOW = 20;   // number of points
    
        ofstream out("C:/Plot Data.txt");
        if (out.fail()) {
            cout << "Failed to open output file" << endl;
            return 1;
        }
    
        for (int I = 0; I < INOW; ++I) {
            out << PLOTX[I] << ' ' << PLOTY[I] << ' ' << PLOTZ[I] << '\n';
        }
    
        out.close();
    }
    

    This will produce a text file with readable values.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.