[Solved] opening file using a stringstream as path?

Hi all,

I have a basic knowledge of C/C++ that has quickly failed me.
In the program below (which is a piece of a larger one that I'm rewriting to C++ from plain C), I'm using stringstreams to merge file paths and file names and store them (trying to be as platform independent as possible) so that I can open them later.

Unfortunately when I try to compile I get the following error three times:

error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)’

The compiler (g++) tells me the lines that have problems are where the files are being opened. I know this isn't working because filename.str() doesn't seem to be a string! (I have tested this by "hard" typing the path, e.g. ifp.open("/home/user/data/") )

In the original C code, this was done using sprintf and variables filename* were defined as arrays of characters. I was hoping C++ had a better way to do this and though it exists, I'm afraid I'm missing something.

Any input is appreciated

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>

using namespace std ;

int main()
{
	//------Variable Declaration------//
	
	int first_file_number, last_file_number, file_counter ;

	string directory = "" ;

	stringstream filename, filenameapd1, filenameapd2, filenamefinaloutput ;

	//------Inputs------//

	cout << "type in the address of the directory where the data is held (e.g. C:\\data\\ or /home/user/data/)" << endl ; 
	getline ( cin , directory ) ;

	cout << "WARNING: the data must be named as data#.asc" << endl << "begin with what file? data" ;
	cin >> first_file_number ;

	cout << "end with what file? data" ;
	cin >> last_file_number ;

	cout << "this program will sequentially analyze data" << first_file_number << " to data" << last_file_number << endl ;
	cout << "hit any key to continue" << endl ;
	cin.get() ;

	file_counter = first_file_number;

	while ( file_counter <= last_file_number )
	{
		//------File Operations------//

		filename << directory << "data" << file_counter << ".asc" ;

		filenameapd1 << directory << "data" << file_counter << "apd1.asc" ;

		filenameapd2 << directory << "data" << file_counter << "apd2.asc" ;

		filenamefinaloutput << directory << "data" << file_counter << "finaloutput_withpowerspec.asc" ;

		cout << "opening file " << filename.str() << endl ;

		file_counter ++ ;

		ifstream ifp ;
		ofstream oAPD_1fp ;
		ofstream oAPD_2fp ;

		ifp.open ( filename.str() ) ;
		oAPD_1fp.open ( filenameapd1.str() ) ;
		oAPD_2fp.open ( filenameapd2.str() ) ;

		if ( !ifp.is_open() ) 
		{
			cout << "the file " << filename.str() << "could not be opened" << endl ;

			break ;

			return 1 ;
		}

	}
	
	return 0 ;

}
Last edited on
Hrm, AFAICS, that should be working...Not sure why it isn't...
Current standards says that fstreams must take char* arguments, use this:
ifp.open ( filename.str().c_str() ) ;
to convert the std::string into a C string
Thank you so much!

filename.str().c_str() did the trick!

I have a somewhat more irrelevant question now. In the code snippet below, I'm trying to have the program standby until any key is pressed. In the original C code this was done using getch() by invoking the conio.h library. I read this: http://www.programmersheaven.com/mb/CandCPP/204855/204855/hit-any-key-to-continue/ and tried using cin.get() or cin.ignore(), but none of them work!

What am I missing here?

1
2
3
cout << "this program will sequentially analyze data" << first_file_number << " to data" << last_file_number << endl ;
cout << "hit any key to continue" << endl ;
cin.get() ;
See: http://www.cplusplus.com/forum/articles/7312/
Notice that if you read using cin >>, operator >> leaves the newline in the stream so you'll have to clear that one to make it work
Thanks again, I changed completely how I get user input so that it is safe.
Topic archived. No new replies allowed.