[英]Ifstream is OK but Ofstream not working in Visual Studio (C++)(WIN11)
當我編寫一個簡單的 ofstream 和 ifstream 實例時,ifstream 正在工作但 ofstream 不工作。 當我運行這個流程序正在創建但我沒有在目錄文件中看到 output.txt。 我嘗試關閉 COMODO Antivirus,但問題仍然存在。 是什么阻止我看到 output 文件?
這是我的 First.cpp:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream output("output.txt");
ifstream input("input.txt");
if (output.is_open()) {
cout << "ouput file is open" << endl;
output << "hi";
}
else {
cout << "output file is not open" << endl;
}
int number;
if (input.is_open()) {
cout << "input file is open" << endl;
input >> number;
cout << "Number:" << number << endl;
}
else {
cout << "input file is not open" << endl;
}
input.close();
output.close();
return 0;
}
這是我的 input.txt:
21
12
22
23
32
當我運行這個程序時:
ouput file is open
input file is open
Number:21
我看到問題與您的防病毒軟件有關,但無論如何我還是會寫這篇文章供其他人閱讀。
在處理 IO 時,重要的是要知道您的可執行文件將從哪個目錄執行。 工作目錄可以在項目配置中設置。 右鍵單擊項目 > 屬性 > 配置屬性 > 調試 > 工作目錄。 配置 Window
工作目錄也是將生成所有應用程序創建的文件的目錄。 例如,示例代碼在項目工作目錄中生成output.txt 。
std::ofstream output("output.txt");
if (!output.is_open())
return -1;
std::cout << "file is open" << std::endl;
output << "Hello there!";
output.flush();
output.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.