Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

input file

Posted on 2003-02-19
6
Medium Priority
?
221 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

581 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