input file

Can you help me, how do you make it to where the user enters the file they want to open. and in the ifstream.open("what_ever_the_user_selected") opens the file
mesicanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ExceterCommented:
char filename[25];
cout << "Enter file name: ";
cin >> filename;
ifstream in;
in.open( filename );
in.close();

Exceter
0
alexanderthegreatCommented:


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

void main(void)
{
     char filenaam[64];
     char lijn[64];

     cout << "Type the name of the file you wish to open: ";
     cin >> filenaam;

     ifstream halen(filenaam);

    if(halen.fail())
     {
          cout << endl;
          cout << "Could not open the file: (" << filenaam << ") " << endl;
     }
     while((! halen.eof()) && (! halen.fail()))
     {
          halen.getline(lijn, sizeof(lijn));
          if(! halen.fail())
               cout << lijn << endl;
     }


}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ExceterCommented:
mesican, I gave you what you asked for...

alexanderthegreat, this is an exessivly complicated method of displaying a file's contents. Additionally, if the file is not found your program will simply try to read from a null file pointer because you did not exit or return from the error checking if statement.

#include <iostream>
#include <fstream>

using namespace std;

void main()
{
    char filenaam[30];
    char c;

    cout << "Type the name of the file you wish to open: ";
    cin >> filenaam;

    ifstream halen(filenaam);

    if( !halan )
    {
         cout << "\nCould not open the file: (" << filenaam << ") " << endl;
         exit(1);
    }
    while( halan )
    {
        in >> c;
        cout << c;
    }
}

Exceter
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

mesicanAuthor Commented:
uhhhh, for some reason I didn't see your post. It was early in the morning, I will give you 20 points.
0
alexanderthegreatCommented:
Thats not correct, when the file does not exists
halen.fail() will become 1 is the same as true,
and the while loop: while((! halen.eof()) && (! halen.fail()))
will not be done because it haves to be false 0 to be executed.
0
ExceterCommented:
>> Thats not correct, when the file does not exists
>> halen.fail() will become 1 is the same as true,
>> and the while loop: while((! halen.eof()) && (! halen.fail()))
>> will not be done because it haves to be false 0 to be executed.

Sorry, for some reason I did not see that. It was late at night. You are correct. :-)

However, what is the advantage of doing it that way? It certainly isn't easier to read.

Exceter
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.