C++ code :: blocks ios :: in Works,ios :: Out | ios :: app否
我有一个非常简单的代码通过Code :: blocks:
#include <iostream>
#include <fstream>
using namespace std;
string firstName, lastName;
int phoneNumber;
int main()
{
fstream file;
file.open("card.txt", ios::in);
if (file.good())
{
cout << "File is good (read)" << endl;
file.close();
}
else
{
cout << "Cannot open file (read)." << endl;
}
file.open("card.txt", ios::out | ios::app);
if (file.good())
{
cout << "Enter your First Name: \n";
cin >> firstName;
cout << "Enter your Last Name: \n";
cin >> lastName;
cout << "Enter your Phone Number: \n";
cin >> phoneNumber;
file << firstName << endl;
file << lastName << endl;
file << phoneNumber << endl;
file.close();
}
else
{
cout << "Cannot open file (write)." << endl;
}
return 0;
}
card.txt文件位于项目主文件夹中。从第一个开始,如果我得到“文件很好(读)”。从第二个开始,我得到“无法打开文件(写)”。
真的不知道为什么。也许代码::阻止属性设置不正确。对我来说,他们似乎还可以,自安装以来就没有改变。
I have a very simple code written in C++ via Code::Blocks:
#include <iostream>
#include <fstream>
using namespace std;
string firstName, lastName;
int phoneNumber;
int main()
{
fstream file;
file.open("card.txt", ios::in);
if (file.good())
{
cout << "File is good (read)" << endl;
file.close();
}
else
{
cout << "Cannot open file (read)." << endl;
}
file.open("card.txt", ios::out | ios::app);
if (file.good())
{
cout << "Enter your First Name: \n";
cin >> firstName;
cout << "Enter your Last Name: \n";
cin >> lastName;
cout << "Enter your Phone Number: \n";
cin >> phoneNumber;
file << firstName << endl;
file << lastName << endl;
file << phoneNumber << endl;
file.close();
}
else
{
cout << "Cannot open file (write)." << endl;
}
return 0;
}
Card.txt file is located in the project main folder. From the first if value I get "File is good (read)". From the second one I get "Cannot open file (write)."
Really don't know why. Maybe Code::Blocks properties are set incorrect. For me they seem ok, they were not changed since installation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论