vinjoy
vinjoy

Reputation: 19

How to output file in function in c++?

I want to output "a" to the file in the function, how do I do it? The following gives me error.

#include <iostream> 
#include <fstream> 

using namespace std;

void outputfile(const char* file);

int main() 
{
    int a = 1;
    ofstream cc;
    cc.open("123.txt");
    if (cc.fail()) 
    {
        cout << "\nError in file opening" << endl;
        return(1);
    }
    outputfile("123.txt");
    cc.close();
    return 0;
}

void outputfile(const char* file)
{
    // I want to output a to the file, how can I do it? The following gives me error.
    file << a;
}

The problem is solved, thanks for answering the question.:)

Upvotes: 0

Views: 2411

Answers (3)

R Sahu
R Sahu

Reputation: 206737

I see couple of issues that need to be addressed.

  1. You can either pass the name of the file to outputfile or the ofstream. If you pass the name of the file to outputfile, the file needs to be opened in outputfile, not in main.

    There are pros and cons to the two approaches. If the name of the file is passed to outputfile, main becomes very simple. However, if the ofstream object needs to be used to make other function calls, it will be better to pass an ofstream as the argument to outputfile.

    If a ofstream is passed to outputfile, outputfile becomes very simple. However, if the ofstream is not going to be used by main for anything else, it is better to leave the details of what mechanism used by outputfile to write the data out. outputfile can even resort to FILE* and fprintf without affecting main.

  2. a needs to be made available to outputfile. It can be made a global variable or be passed to outputfile. It is better to avoid global data. Hence, prefer to pass it as an argument.

Solution 1

Pass an ofstream to outputfile, in addition to a.

#include <iostream> 
#include <fstream> 

using namespace std;

void outputfile(std::ofstream& file, int a);

int main() 
{
   int a = 1;
   ofstream cc;
   cc.open("123.txt");
   if (cc.fail()) 
   {
      cout << "\nError in file opening" << endl;
      return(1);
   }
   outputfile(cc, a);

   // No need. The file will be closed when the function returns.
   // cc.close();

   return 0;
}

void outputfile(std::ofstream& file, int a)
{
    file << a;
}

Solution 2

The details of opening and closing the file is left to outputfile. Here, outputfile uses ofstream.

#include <iostream> 
#include <fstream> 

using namespace std;

void outputfile(char const* file, int a);

int main() 
{
   int a = 1;
   outputfile("123.txt", a);

   return 0;
}

void outputfile(char const* file, int a)
{
   ofstream cc(file);
   if (cc.fail()) 
   {
      cout << "\nError in file opening" << endl;
      return;
   }
   cc << a;
}

Solution 3

The details of opening and closing the file is left to outputfile. Here, outputfile uses FILE*.

#include <iostream> 
#include <fstream> 

using namespace std;

void outputfile(char const* file, int a);

int main() 
{
   int a = 1;
   outputfile("123.txt", a);

   return 0;
}

void outputfile(char const* file, int a)
{
   FILE* fp = fopen(file, "w");
   if ( fp == NULL )
   {
      cout << "\nError in file opening" << endl;
      return;
   }
   fprintf(fp, "%d", a);
   fclose(fp);
}

Upvotes: 3

Paul Evans
Paul Evans

Reputation: 27577

a isn't in scope. Either declare it outside of main() in global space, or pass it into outputfile, something like:

void outputfile(ofstream &file file, int a) {
    file << a;
}

Upvotes: 1

Enamul Hassan
Enamul Hassan

Reputation: 5445

At first, you have declared a in main(). Either you have to pass it as parameter or you have to take it global. I took global in the code below.

Then what do you do passing const char*? You have to write in file, so send the stream instead like below:

#include <iostream>
#include <fstream>
using namespace std;
 void outputfile(ofstream &file);  int a=1;
 int main() {

  ofstream cc;
  cc.open("123.txt");
  if (cc.fail()) {
    cout <<"\nError in file opening" << endl;
    return(1);
  }
  outputfile(cc);
  cc.close();
  return 0;
}
void outputfile(ofstream &file)
{
  // I want to output a to the file, how do I do it? The following gives me error.
  file << a;
}

Upvotes: 2

Related Questions