answersLogoWhite

0


Best Answer

#include<iostream>

#include<fstream>

#include<string>

#include<vector>

struct data

{

std::string lastname;

std::string firstname;

double weight;

};

std::istream& operator>> (std::istream& is, data& d)

{

return is >> d.lastname >> d.firstname >> d.weight;

}

std::ostream& operator<< (std::ostream& os, const data& d)

{

return os << d.lastname << ' ' << d.firstname << ' ' << d.weight;

}

int main()

{

std::cout << "Enter filename: ";

std::string filename;

std::cin >> filename;

std::ifstream infile (filename);

if (infile.bad())

{

std::cerr << filename << " could not be opened!\n";

return -1;

}

std::vector<data> v;

char c;

while (infile >> c)

{

infile.putback (c);

data d;

infile >> d;

v.push_back (d);

}

infile.close();

for (auto d : v)

std::cout << d << '\n';

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c plus plus program that prints an error message and exits when an ifstream cannot be opened?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why the error messag -The custom form cannot be opened Outlook will use an Outlook form instead. the form required to view this message cannot be displayed Contact your administrator?

Try logging in as the Administrator. If this is not possible, then as the message said, contact your administrator.


What does it mean 'could not be opened because the associated helper application does not exist change the association in your preferences'?

This error message indicates that your computer cannot locate the program that is used to open/run a certain file. The message is suggesting that you 'change the association'. This means that one way of remedying the problem is by picking a new program to open/file. You can do this by right clicking on the file and following the following instructions: Properties &gt; General &gt; Click on 'Opens with' and select a new program


Why handy cafe server cannot be open?

If your Handy Cafe server cannot be opened, try erasing the program and re-downloading it. At times, there is a problem with the initial download.


Write a Program to open close a file in c?

You have to use the preprocessor directive fstream.h. #include &lt;fstream.h&gt; void main () { ifstream inDataStream;//the stream to open excess to a file inDataStrea.opem("fileName.txt");//to open an excess to the file fileName.txt (can be //any) if (inDataStream.fail())//check if the file was opened successfully { cout &lt;&lt; "Couldn't open the file!"; } inDataStream &lt;&lt; "Hello";// writing the word "Hello" in the file (can be any information) inDataStream.close();//close the opened excess to the file }


A personal account cannot be opened in?

One place in which a personal account cannot be opened is the Reserve Bank of India.


What alerts a sender that a message has been opened?

the picture on the screen


Are there mods for zoo tycoon?

Yes. A lot of mods have mysteriously disappeared over the internet since I last checked so there are rarely any good ones, all of the good ones have turned up missing. Sometimes if you try to download mods a message will display "The file cannot be opened, we are searching for a program to open it" meaning you might need a program like adobe or something. Happens a lot to me. Hope I helped :)


What program did Edison college have when it first opened?

what areas of study did edison college have then they first opened


A is a program that secretly attaches inself to a carrier as a document or program and then execute when that document is opened or program is launched?

virus


Is there a software program that can read text messages?

I don't believe so. On some phones there is an option that narrates text when a text message is opened. This is however available only on some smart phones. Hope this helped!


Is a software application program?

If you opened a spreadsheet, you will be running a software program. If you use Microsoft Windows, it will probably be Excel.


What is exeption in c sharp?

an exception is an error.it is usual to use exception handlers in your program to guard against crashesfor exampletry{DatabaseConnection.Open();}catch(Exception ex){MessageBox.Show(ex.Message);}finally{DatabaseConnection.Close();}This attempts to open a connection and if there is an error it shows a message box with the pre defined message for that exception. then it cleans up by closing the connection if any part was opened. this stops the program from crashing all the time.