0

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.

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Salem
0

> 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.

0

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.