Input from text file from a specific location

Good morning, friends of the elevated entertainment.

I am trying to write a program that imports data from a text file from a specific location on my hard drive, store that data in a vector, and then have it print that data in the command window. The code as is fails to open the text file. Could you explain to me why, and what I need to change?

Also, originally I wanted the vector and the variable xvalue to be of type double, since the data in simplefile.txt is a column of numbers, but when I put double to all the positions that now read string, the program didn`t compile. Do you have any clue why that is?

Thank you very much for your help!

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
//Program is supposed to load data from a text file, store it in a vector, and then output the vector to the command window

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <istream>
#include <algorithm>
#include <iterator>

using namespace std; 

int main()
{
	vector<string> data;

	string xvalue;

	ifstream inputfile("D:\Sebisoft Erweiterung\Simplefile.txt");
if(!inputfile) //Always test the file open.
    {
        cout<<"Error opening input file"<<endl;
        system("pause");
        return -1;
    }
while (!inputfile.eof())
    {
		getline(inputfile, xvalue);
        data.push_back(xvalue);
    }
copy(data.begin(), data.end(), ostream_iterator<string>(cout, " "));

return 0;
}
Backslash is a bit special because it is used to escape characters, which can be used to create special characters. \n is a newline character, \t is a tab character, and so on. In your file path \S will be treated as some kind of special character. This is not what you want.

To get a backslash character you need to escape it first by putting backslash in front of it.
 
ifstream inputfile("D:\\Sebisoft Erweiterung\\Simplefile.txt");

When dealing with file paths it's often easier to use forward slashes instead.
 
ifstream inputfile("D:/Sebisoft Erweiterung/Simplefile.txt");
Thank you very much, now it works.
Topic archived. No new replies allowed.