Reading and writing into a file

Hi guys:

I am creating this code to read files. The problem I am having is the simple task of opening and writing into a file. I know I could create the code to open a certain file, but I want to enter different files and I dont want to create 10 different codes to open different text files. I am getting an error which I am not sure how to correct. Thanks for the help,

Gus

My code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
	string fileToRead;
	string fileToWrite;
	int numberImages;
	cout << "Enter name of input file from IMEXAMINE: ";
	cin >> fileToRead;
	cout << "Enter name for output file for IMCOMBINE: ";
	cin >> fileToWrite;
	cout << "How many images in this file: ";
	cin >> numberImages;
	ifstream in(fileToRead);
	ofstream out(fileToWrite);
	double col, line, r, mag, flux, sky, peak, e, pa, beta, enclosed, moffat, direct;
	vector<double> coordX (numberImages);
	vector<double> coordY (numberImages);
	vector<double> resultX (numberImages);
	vector<double> resultY (numberImages);
	for(int i=0;i<numberImages;i++)
	{
		if (in >>col>>line>>coordX[i]>>coordY[i]>>r>>mag>>flux>>sky>>peak>>e>>pa>>beta>>enclosed>>moffat>>direct) 
		{
			resultX[i] = coordX[0] - coordX[i];
			resultY[i] = coordY[0] - coordY[i];
			out << resultX[i] << " " << resultY[i] << endl;
		}
	}
	return 0;
}


The error
1
2
3
4
5
6
7
8
9
10
11
% g++ -o comb_shift comb_shift.cpp
comb_shift.cpp: In function 'int main()':
comb_shift.cpp:20: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)'
/usr/include/c++/4.2.1/fstream:465: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/fstream:451: note:                 std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/iosfwd:89: note:                 std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
comb_shift.cpp:21: error: no matching function for call to 'std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)'
/usr/include/c++/4.2.1/fstream:596: note: candidates are: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/fstream:580: note:                 std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/iosfwd:92: note:                 std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)
%


The file is supposed to be reading
1
2
3
4
5
6
7
8
 441.57  483.36 441.57 483.36
  11.69  12.53  96980.   213.6   4032. 0.21   89 5.40     3.87     3.98   3.90
 439.33  485.18 439.33 485.18
  12.32  12.50 100119.   216.5   3866. 0.15   85 7.42     4.04     4.12   4.11
 438.87  486.08 438.87 486.08
   9.98  12.61  90390.   215.7   5085. 0.28  -74 4.06     3.39     3.47   3.33
 435.59  485.58 435.59 485.58
  14.05  12.44 105277.   216.1   3057. 0.10   56 8.62     4.53     4.69   4.68
The ifstream and ofstream constructors take a C-style string, not std::string.

Try:
1
2
ifstream in( fileToRead.c_str() );
ofstream out( fileToWrite.c_str() );
Last edited on
I've run into a simular error. I solve this by entering in the file name as a char array and using a pointer to change the characters.
1
2
3
ifstream inFile;
ofstream outFile;
inFile.open("fileNameHere");

do not forget to close the file
inFile.close();
Topic archived. No new replies allowed.