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?
//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>
usingnamespace 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.