Okay I am SO new to C++ and I'm trying to write a code where the user can input the file name to read from. I have looked at a million forums but it doesn't seem to work for me. The program compiles but it is not getting the correct values from the file. This is a simplified version of what I have.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    double span;
    string inputfilename;
    ifstream inputfile;
    cout <<"Enter file name" <<endl;
    cin>>inputfilename;
    inputfile.open(inputfilename.c_str());
    inputfile>>span;
    cout<<span;
    inputfile.close();
    system("PAUSE");
    return 0;
}

Recommended Answers

All 8 Replies

You would do well with some error handling, as I suspect the file isn't being opened and span remains uninitialized:

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

using namespace std;

int main()
{
    string filename;

    cout << "Enter a file path: ";
    getline(cin, filename);

    ifstream in(filename.c_str());

    if (!in)
        perror("Error opening requested file");
    else {
        double span;

        if (!(in >> span))
            cerr << "Invalid file format (expected floating point value)\n";
        else
            cout << "span = " << span << '\n';
    }
}

Looks correct. It does indeed work.

Make a file on your desktop called Foo.txt. Put the value 10 in it.

Run your program and type exactly this:

C:/Users/Username/Desktop/Foo.txt

Replace Username with your username.. It'll print 10.

Also note that the method you're currently reading with will break as soon as it hits the first Space or Newline character.

Deceptikon, I copied and pasted your code and still would not work. It says error opening file. Is there something wrong with my input file? It's a word document called isection, so when asked for the path I type isection.txt Do I need to type the entire path as triumphost suggested?

Okay I tried typing the entire path and it did indeed work, thanks! But question, on a previous program I remember only typing "inputfile.txt" or something of the sort. Is there a reason I can not do that with this code?

You dont have to put the entire file path if the file lives in the same folder as the .exe file. Only if it is somewhere else than you need to.

Thanks so much!

Is there a reason I can not do that with this code?

Probably because the file isn't in the same directory as the .exe, as already explained.

Alternatively you have debug turned on and it needs to be in the debug diectory of the project.

Deceptikon, I copied and pasted your code and still would not work. It says error opening file.

That's the entire point of my code: to add error handling that tells you what's wrong. I didn't fix the problem because it's obviously a case of user error in typing the correct file path. But knowing that the error is "file not found", or at the very least "cannot open file" points you in the direction of how to fix the problem. That's why catching and reporting errors is so important. Otherwise you're sitting in the dark with no clue of how to proceed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.