I have written a function which will open the file with the input name provided.I am compiling on a Linux box using g++ compiler.Detail code below Plz let me know what is the wrong in code.

1  #include<iostream>
      2  #include<fstream>
      3  #include<string>
      4  #include<stddef.h>
      5  #include<cstddef>
      6  #include<cstring>
      7
      8  using namespace std;
      9
     10  int main ()
     11 {
     12 string file1,file2;
     13 string file3;
     14
     15 fstream OpFile(string,ios_base::openmode);
     16
     17 cout << "Enter File Names"<<"\n";
     18 getline(cin,file1);
     19 getline(cin,file2);
     20 getline(cin,file3);
     21
     22 fstream INPUT1;
     23 fstream INPUT2;
     24 fstream INPUT3;
     25
     26 INPUT1 =  OpFile(file1, ios::in | ios::binary);
     27 INPUT2 =  OpFile(file2, ios::in);
     28 INPUT3 = OpFile (file3, ios::out);
     29
     30
     31 INPUT1.close();
     32 INPUT2.close();
     33 INPUT3.close();
     34
     35 return 0;
     36 }
     37
     38      fstream OpFile (string filename, ios_base::openmode mode)
     39     {
     40          fstream file;
     41
     42         file.open (filename.c_str(), mode);
     43         return file;
     44     }

Part of the error message I m getting is-

ex.cpp: In function `int main()':
ex.cpp:26: error: no match for 'operator=' in 'INPUT1 = OpenFile(std::string, std::_Ios_Openmode)(std::operator|( _S_in, _S_bin))'
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/iosfwd:90: note: candidates are: std::basic_ifstream<char, std::char_traits<char> >& std::basic_ifstream<char, std::char_traits<char> >::operator=(const std::basic_ifstream<char, std::char_traits<char> >&)
ex.cpp: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/ios_base.h:781: error: `std::ios_base::ios_base(const std::ios_base&)' is private

Recommended Answers

All 3 Replies

I think the major extent of your problem takes place at lines #26 thru 28.

The <fstream> class does not include an overloaded assignment operator. I believe the only operators overloaded for this class are the << insertion, >> extraction, and ! 'not' operators. Everything else (including the = assignment operator) is unusable.

So, in your attempt to assign the resulting 'return' from OpFile() to your 'INPUT" object (which I believe is incorrectly declared as 'fstream' as opposed to 'ifstream'), the compiler does not know how to resolve this assignment operation as it is an aggregate data type.

Suggestion:
Make OpFile() a 'void' function. Pass in your <fstream> object into this function along with a string file name. Perform the open operation inside of the function.

What you are essentially doing is making a 'wrapper' for the open() function... your other option would be just to explicitly open the files directly from your ifstream objects in main().

There are other issues with your code. I'm sure let us know about them in due time.

After working on the suggestions also m facing same problem,r the other issues mentioned by you r causing problem.
modified code is.

1 #include<iostream>
      2 #include<fstream>
      3 #include<string>
      4 #include<stddef.h>
      5 #include<cstddef>
     6 #include<cstring>
      7
      8 using namespace std;
      9
     10 int main ()
     11 {
     12         string file1,file2;
     13         string file3;
     14
     15         void OpenFile(fstream,string,std::ios_base::openmode);
     16
     17         cout << "Enter File Names"<<"\n";
     18         getline(cin,file1);
     19         getline(cin,file2);
     20         getline(cin,file3);
     21
     22         fstream INPUT1;
     23         fstream INPUT2;
     24         fstream INPUT3;
     25
     26         OpenFile(INPUT1,file1, std::ios_base::in | std::ios_base::binary);
     27         OpenFile(INPUT2,file2, std::ios_base::in);
     28         OpenFile(INPUT3,file3, std::ios_base::out);
     29
     30
     31         INPUT1.close();
     32         INPUT2.close();
     33         INPUT3.close();
     34
     35         return 0;
     36 }
     37
     38 void OpenFile (fstream file ,string fileName, std::ios_base::openmode mode)
     39 {
     40       //  fstream file;
     41
     42         file.open (fileName.c_str(), mode);
     43         if (!file.is_open())
     44         {
     45          cout << "Couldn't open file: "<<endl;
     46         }
     47         //return file;
     48 }

Error message is-

ex.cpp: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/bits/ios_base.h:781: error: `std::ios_base::ios_base(const std::ios_base&)' is private
ex.cpp:26: error: within this context
ex.cpp: In copy constructor `std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)':
/usr/lib/gcc/i386-redhat-linux/3.4.3/../../../../include/c++/3.4.3/streambuf:769: error: `std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
ex.cpp:26: error: within this context
ex.cpp: In function `int main()':
ex.cpp:26: error: initializing argument 1 of `void OpenFile(std::fstream, std::string, std::_Ios_Openmode)'


It would be helpful if u reply with the solution.

I'm not sure if you are learning a different style of file i/o, but it's like nothing I've ever seen.

This is the general procedure I've been taught:

1. create an 'ifstream' object (to read from a file)
2. attempt to open a file.
3. perform error checking (to see if file actually opened)
4. if file open successful, read the file into a data structure of your choice (string or vector seem to be popular)
5. close the ifstream object.
6. perform desired operations with the newly loaded data.
7. create an 'ofstream' object (for writing to a file)
8. open (or create new) a file.
9. write to the file.
10. close the ofstream object.

Now, with that out of the way, you have even more fundamental issues:

//Try this:
void OpenFile(fstream& file, string& fileName)
 {     
     file.open(fileName.c_str(), ios::in|ios::trunc);    

     if (!file.is_open())
     {
          cout << "\a\nCouldn't open file..!!!";
          cout <<"\nEnsure that file exists, is in the proper location, has not been renamed or deleted.";
          cout << "\nProgram will now terminate in preparation for file troubleshooting... ";
          cout << "\nPress [Enter] to exit program ";
          cin.get();
          exit(EXIT_FAILURE);
     }
}
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.