title: Using fstream in VIsual C++
body: <p>I am trying to use fstream but am running into problems when trying to open a file from within Visual Studio 2013. In Visual Studio, I have two resources that I have enabled to be used in the project titeld input1.txt and input2.txt . If I directly run the application from the Debug folder using File Explorer, I am able to use the ifles. If I try to run it from within Visual Studio with ctrl, neither files can be found. I believe my code is correct, but I'm not sure what changes to make to the project to have it run correctly.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  const bool VERBOSE(true);
  
  int main(){
  
  ifstream input;
  ofstream output;
  
  string inFileName;
  string outFileName;
  string tempString;
  
  // Get input file name into a string
  cout &lt;&lt; "Input file name: " &lt;&lt; flush;
  cin &gt;&gt; inFileName;
  
  if (VERBOSE)
  {
      cout &lt;&lt; "Input file name is " &lt;&lt; inFileName &lt;&lt; endl;
  }
  
  // Convert filenames to C strings and use stream.open()
  input.open(inFileName.c_str());
  if (input.fail())
  {
      cout &lt;&lt; "File " &lt;&lt; inFileName &lt;&lt; " cannot be opened" &lt;&lt; endl;
      return -1;
  }
  
  // Get output file name into a string
  cout &lt;&lt; "Output file name: " &lt;&lt; flush;
  cin &gt;&gt; outFileName;
  
  if (VERBOSE)
  {
      cout &lt;&lt; "Output file name is " &lt;&lt; outFileName &lt;&lt; endl;
  }
  
  // Convert filenames to C strings and use stream.open()
  // When opening output file, it will create the file if it
  // does not exist, and will clobber it if it does.
  output.open(outFileName.c_str());
  if (output.fail())
  {
      cout &lt;&lt; "File " &lt;&lt; outFileName &lt;&lt; " cannot be opened" &lt;&lt; endl;
      return -2;
  }
  
  // While there is more to the input file, get a word 
  //   and copy it to the output file on its own line.
  while (!input.eof())
  {
      input &gt;&gt; tempString;
      if (VERBOSE)
      {
          cout &lt;&lt; " Length is " &lt;&lt; tempString.length() &lt;&lt; " for " &lt;&lt; flush;
      }
  
      if (tempString.length() &gt; 0)
      {
          if (VERBOSE)
          {
              cout &lt;&lt; tempString &lt;&lt; endl;
          }
          output &lt;&lt; tempString &lt;&lt; endl;
      }
      else
      {
          if (VERBOSE)
          {
              cout &lt;&lt; "No more input" &lt;&lt; endl;
          }
      }
  
      // This is needed to keep the last non-whitespace word
      // read in from being printed twice if the file ends in
      // whitespace, including a newline.
      tempString.clear();
  }
  cout &lt;&lt; endl;
  
  return 0;
  </code></pre>
  
  <p>}</p>