0

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.

#include <iostream>
#include <fstream>
#include <string>

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 << "Input file name: " << flush;
cin >> inFileName;

if (VERBOSE)
{
    cout << "Input file name is " << inFileName << endl;
}

// Convert filenames to C strings and use stream.open()
input.open(inFileName.c_str());
if (input.fail())
{
    cout << "File " << inFileName << " cannot be opened" << endl;
    return -1;
}

// Get output file name into a string
cout << "Output file name: " << flush;
cin >> outFileName;

if (VERBOSE)
{
    cout << "Output file name is " << outFileName << 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 << "File " << outFileName << " cannot be opened" << 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 >> tempString;
    if (VERBOSE)
    {
        cout << " Length is " << tempString.length() << " for " << flush;
    }

    if (tempString.length() > 0)
    {
        if (VERBOSE)
        {
            cout << tempString << endl;
        }
        output << tempString << endl;
    }
    else
    {
        if (VERBOSE)
        {
            cout << "No more input" << 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 << endl;

return 0;

}

  • Are you providing a full path to the file, or a relative path or name you expect to be in the local directory? – Michael Urman May 28 '14 at 23:32
  • Once you fix that, [this: **`while (!input.eof())`**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) is wrong. Sooner or later you're going to have to address that. – WhozCraig May 29 '14 at 00:43
  • I was just trying to use the name of the file with its extension originally. It works when I use the full name. This code was actually given to us by our teacher to understand how file I/O but I will certainly look into the while loop. – When will it be my turn May 29 '14 at 00:58
  • @InflictedOwned for your posted code, the symptom you can demonstrate to your instructor is what happens when `tempString.clear()` is *not* done at the bottom of the file. That has nothing to do with reading the *file* and is a poor idea. The expression `input >> tempString` should be checked for success; not the eof bit. The latter *can* be checked *after* the loop to ensure you completed reading the file. See [**`std::ios_base::iostate`**](http://en.cppreference.com/w/cpp/io/ios_base/iostate) for more info on the state bits of a standard stream and how they behave. – WhozCraig May 30 '14 at 01:00

1 Answers1

0

Specify the full path to the files when you cin them.

vsoftco
  • 55,410
  • 12
  • 139
  • 252