c++ ofstream 文件不存在_C++用ofstream创建的文件,无法打开的情形

C++用ofstream创建的文件,无法打开的情形代码里有else { // Couldn't open the file. std::cout << "The file could not be opened!\n"; return 1; //

C++用ofstream创建的文件,无法打开的情形代码里有else { // Couldn't open the file. std::cout << "The file could not be opened!\n"; return 1; // Indicates a problem occurred. }我想知道什么时候会发生这种情况,书上建议输入一个非法的文件路径试试看,要怎么做啊?// quote2.cpp - Script 4.8// We need the iostream file in order// to use cout and cin.#include // We need the string file// for the string functionality.#include // We need the fstream file in order// to work with files.#include // Start the main function.int main() { // Declare the necessary variables. std::string quote, speaker; // Prompt the user for the quotation. std::cout << "Enter a quotation (without quotation marks):\n"; std::getline(std::cin, quote); // No extraneous input to be discarded // because all input is assigned to the string! // Prompt the user for the quotation's author. std::cout << "Enter the person to whom this quote is attributed:\n"; std::getline(std::cin, speaker); // Create a blank line in the output. std::cout << "\n"; // Repeat the input back to the user. std::cout << "The following quote has been received...\n\n" << quote << "\n-" << speaker << "\n\n"; // Write the data to the file. // Define a file object. std::ofstream fileOutput("quotes.pdf", std::ios::app); // If the file is open, record the data. if ( _open()) { // Write the data to the file. fileOutput << quote << "|" << speaker << "\n"; // Close the stream. ose(); // Print a message. std::cout << "The data has been written to the file!\n"; } else { // Couldn't open the file. std::cout << "The file could not be opened!\n"; return 1; // Indicates a problem occurred. } // Wait for the user to press Enter or Return. std::cout << "Press Enter or Return to continue.\n"; std:: t(); // Return the value 0 to indicate no problems. return 0;} // End of the main() function.

展开

全部

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值