fstream, path in a variable, how to?

I'm writing a program that reads from a file, but I'd like the path to the file to be dynamic. I created testing variables first to see if I can even get them to work properly.
The program compiles, but Visual Studio 2010 gives me an error afterward:
"Unhandled exception at 0x5be7d51c (msvcr100d.dll) in read-write.exe:
0xC0000005: Access violation reading location 0x2eaa81e6"
It opens strlen.asm, with arrow pointed to "str_misaligned"

I'm using these header files:
-iostream
-fstream
-string
-stdlib.h
-vector

I tried with these two versions:
1
2
3
4
5
//...
	string readFile = "read" + '.txt';
//...
	ifstream fin(readFile);
//... 


I thought that maybe it's missing quote signs:
1
2
3
4
5
//...
	string readFile = "\"read" + '.txt' + '\"';
//...
	ifstream fin(readFile);
//... 


But now I think that using string-type variables might be idiotic for something like that. I don't know what else to use though.
fstream constructor take a C string. So you'll need to call c_str to get it from a C++ string
1
2
string readFile = "read" + '.txt';
ifstream fin(readFile.c_str());
Doesn't work, but it gives me different hexadecimal numbers, same .dll file and opens strlen.asm again, but doesn't highlight anything in it this time.

Here is my full code:
(it's a work in progress, that's why some things may seem weird or unnecessary)

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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> 
#include <vector>
using namespace std;

int main() {
//Defining file paths
	string readFile = "read" + '.txt';
	string writeFile = "write" + '.txt';
//Creating string array and integer array
	const int size = 100; //number of *rows*
	string str[3][size];
	unsigned int intgr [3][size];
	int a = 0, b = 0; //array coordinates, a=*columns*, b=*rows*

//[READING FROM A FILE]
	ifstream fin(readFile.c_str());
	if(!fin) {
		cout << "Cannot open file.\n";
		cin >> a;
		return 0;
	}
	//Putting values into the string array, converting values to the integer array
	while (!fin.eof()) {
		a = 0;
		getline(fin, str[a][b],'	');
		intgr[a][b] = atoi(str[a][b].c_str());
		a++;
		getline(fin, str[a][b],'	');
		intgr[a][b] = atoi(str[a][b].c_str());
		a++;
		getline(fin, str[a][b],'\n');
		intgr[a][b] = atoi(str[a][b].c_str());
		b++;
	}
	//------------------------------------------------------------
	unsigned int result = intgr[2][0] * intgr[0][1];
	cout << result << endl;
	fin.close();

//[WRITING TO A FILE]
	ofstream fout(writeFile.c_str());
	cin >> a;
	return 0;
}


Everything worked as it should until I decided to make variables for file paths. Nothing else was changed, so the rest should be fine.
It works if I write string readFile = "read.txt"; instead of string readFile = "read" + '.txt';

Is there any other thing that I can use instead of string operator "+"?


You are using single quote around your extension. It should be double quotes:

1
2
3
string readFile = "read" + '.txt'; // wrong

string readFile = "read" + ".txt"; // right 


EDIT:
Whoops, its still wrong ;o)

Try this:
 
string readFile = string("read") + ".txt"; // right 

Last edited on
Yup, works! Thanks a lot!

I've used single quotes because of this: http://www.cplusplus.com/reference/string/operator+/
So when are single quotes used?
single quotes are for single characters or integers, double quotes for strings
Topic archived. No new replies allowed.