how can i make a new file and acces it from a class object?
i refferd some book but i am not clear about it.

Recommended Answers

All 3 Replies

> how can i make a new file and acces it from a class object?
to make a file and access it:

#include <iostream>
#include <fstream>
#include <string>
using namespace std ;

int main()
{
  const char* const file_name = "/tmp/my_newfile" ;
  // make a new file
  {
    ofstream file(file_name) ;
  }
  
  // access it (write)
  {
    ofstream file(file_name) ;
    file << "this is a test\nline 2\nlast line\n" ;
  }
  
  // access it (read)
  {
    ifstream file(file_name) ;
    string line ;
    while( getline(file,line) ) cout << line << '\n' ;
  }
}

the same code could also be in a member function of a class.

Can you please clarify what this statement means.

const char* const file_name = "/tmp/my_newfile" ;

If it is for making file why cant we make a file using ofstream only?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.