?
Solved

input file

Posted on 2003-02-19
6
Medium Priority
?
218 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:mesican
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

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

Exceter
0
 

Accepted Solution

by:
alexanderthegreat earned 80 total points
ID: 7986966


#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
 
LVL 8

Expert Comment

by:Exceter
ID: 7989219
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mesican
ID: 7990574
uhhhh, for some reason I didn't see your post. It was early in the morning, I will give you 20 points.
0
 

Expert Comment

by:alexanderthegreat
ID: 8000056
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
 
LVL 8

Expert Comment

by:Exceter
ID: 8000448
>> 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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question