my program is basically to ask the user to input the number of input files and the name of each input file. Then, it concatenates every line of input files with same line number into a complete line. All the complete lines are output into an output file with name specified by the user.
The following is my code:
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 usingnamespace std;
5
6 int main(){
7
8 int fileNum;
9 string inFileName, outFileName;
10
11 cout << "How many input files: ";
12 cin >> fileNum;
13
14 //create an array of ifstream objects
15 ifstream *inFilePtr = new ifstream[fileNum];
16
17 //open every input file
18 for (int i = 0; i < fileNum; i++){
19 cout << "Name of file NO." << (i + 1) << ": ";
20 cin >> inFileName;
21 (inFilePtr[i]).open(inFileName);
22 }
23
24 //create an ofstream object
25 cout << "Specify the name of output file: ";
26 cin >> outFileName;
27 ofstream oFile;//(outFileName);
28 oFile.open(outFileName);
29
30 //create an array of strings to store lines in each file
31 string *stringPtr = new string[fileNum];
32
33 //concatenate each line
34 while (getline(*inFilePtr, *stringPtr)){
35 oFile << *stringPtr << '\t';
36 for (int i = 1; i < fileNum; i++){
37 getline(inFilePtr[i], stringPtr[i]);
38 oFile << stringPtr[i] << '\t';
39 }
40 oFile << endl;
41 }
42
43 //close input and output files
44 for (int i = 0; i < fileNum; i++)
45 inFilePtr[i].close();
46 oFile.close();
47
48 //delete the dynamically allocated arrays
49 delete[] inFilePtr;
50 delete[] stringPtr;
51
52 return 0;
53 }
My problem is: when I compile, some error occurs with the underlined code, "error: no matching function for call to std::basic_ifstream<char>::open(std::string&)’".
can anyone explain why?
many thanks.
Before C++11 there was no overload of open that takes a std::string as argument, only a C string (const char*). You can use the c_str() member function to get a C string from a std::string. (inFilePtr[i]).open(inFileName.c_str());