Problem : For years in Fortran95, I've been reading and writing files into various directories of my choice, thus keeping our data files in an orderly fashion. However, now I am shocked to find that you can't do anything like that in C++.
Please tell me if there is a way, and give me an example.
Environment : Intel C++ compiler for Linux*
Resolution : This is not a compiler issue. You need to have suitable access rights to the file. You can take help from system administrator to get suitable access rights for the file. I am providing a sample test case that I tried at my end in FC11 box. It works without issue.
// file_rw.cpp
//
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char str[2000] ;
fstream fs( "/testdir/prt1.txt", ios::in ) ;
if(!fs) {
cout << "Cannot open input file.\n";
return 1;
}
fstream ft( "/testdir/prt2.txt", ios::out | ios::in ) ;
if(!ft) {
cout << "Cannot open output file.\n";
return 1;
}
while ( !fs.eof() )
{
fs.getline(str, 2000 ) ;
ft << str << endl;
}
fs.close () ;
ft.close ();
return 0;
}