0

I am trying to output a file with the values of an array written in columns. Here is a simple reproducible version of my code:

# include <iostream>
# include <fstream>
# include <math.h>
# include <iomanip>
# include <cmath>
# include <stdlib.h>
# include <cstdlib>
//# include <fstream.h>
# include <string.h>
# include <string>
//# include <dos.h> //For Sleep() 

using namespace std;
int main()
{
    char outputFileName[30] = "output.dat";
    int NumOfRows = 5;
    int nCount[5] = {0, 1, 2, 3, 4};
    int nCount2[5] = {1, 2, 3, 4, 5};
    
    cout<<"Creating output file"<<endl;
    ofstream outFile(outputFileName);
    outFile<<"1st Param"<<setw(15)<<"2nd Param"<<endl; //Header
        for (int a = 1; a < NumOfRows; a++){
            
            // Reading rest of file
            outFile << nCount[a] << nCount2[a];

                
        }
    outFile<<""<<endl;
    
    return 0;
}

I am running this using CINT/ROOT C/C++ Interpreter version 5.18.00. However, when I run this, there is no output.dat produced. I have tried searching for it but it is nowhere on my computer. Any suggestions?

Edit: I have added the following to the code to check the output file

            // Warning if file cant be opened
    if(!outFile.is_open()){ 
        cout << "Error opening file. \n";
        //cout << "Giving Retry... \n";
    }
    if(outFile.is_open()){
        cout<<"Output File was opened successfully"<<endl;
    }
    if(outFile.good()){
        cout<<"Output File is ready for reading"<<endl;
    }
    outFile.close();
    
    if(!outFile.is_open()){
        cout<<"Output File closed successfully"<<endl;
    }

When I run the code now, this is the output:

Creating output file
Output File was opened successfully
Output File is ready for reading
Output File closed successfully

The output.dat is still missing however.

Woj
  • 449
  • 1
  • 5
  • 15
  • 1
    Check the stream state after every IO transaction. It's entirely possible you aren't even successfully opening the file. If you don't check for success, you won't know when you fail, and that makes debugging much, much harder. – user4581301 Aug 28 '20 at 17:06
  • Side note. I see it's commented out, but dos.h is support for the old DOS operating system of the 1980s. Very old. Very obsolete. Best to find a modern replacement and carefully consider not using sources that recommend you use it. For `Sleep` you should use [`std::this_thread::sleep_for`](https://en.cppreference.com/w/cpp/thread/sleep_for) from the `` header in a modern C++ program. – user4581301 Aug 28 '20 at 17:09
  • Do you really have to use a c++ interpreter instead of an IDE and compiler. – drescherjm Aug 28 '20 at 17:18
  • 2
    Maybe you want to write a small program and just print the current working directory: [https://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from](https://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from) – drescherjm Aug 28 '20 at 17:31
  • @drescherjm Yes, it is a requirement for my course. – Woj Aug 28 '20 at 17:47
  • @user4581301 Alright, I have added lines to check. – Woj Aug 28 '20 at 17:52
  • Are you sure you're looking in the run directory? –  Aug 28 '20 at 17:56
  • Out of my previous link this answer: [https://stackoverflow.com/a/143187/487892](https://stackoverflow.com/a/143187/487892) or [https://stackoverflow.com/a/143188/487892](https://stackoverflow.com/a/143188/487892) are likely ones for you to try. Some of the other ones probably will not work. – drescherjm Aug 28 '20 at 18:01
  • @Chipster I am not sure. I am trying to write a program to get the current working directory. – Woj Aug 29 '20 at 01:44

0 Answers0