C++ 使用ifstream、ofstream和fstream
试图把“牛肉”这个词读入文件C++ 使用ifstream、ofstream和fstream,c++,fstream,ifstream,ofstream,C++,Fstream,Ifstream,Ofstream,试图把“牛肉”这个词读入文件 稍后,将文件内容编辑为用户想要的任何内容,这些内容将存储在字符串“line”中 该文件从不显示,当我手动检查时,文本文件为空 #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; int main() { 字符串行={'}; 流文件1; file1.open(“beef.txt”); 此代码段中的文件1: if (file1.is_open()) { cout << "Enter what you would like to
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串行={'};
流文件1;
file1.open(“beef.txt”);
此代码段中的文件1:
if (file1.is_open())
{
cout << "Enter what you would like to be contained in the file" << endl;
cin >> line;
ofstream file1;
file1 << line << endl;
}
这会隐藏您以前创建的文件。此外,您现在隐藏的对象包含指向“beef.txt”
的有效文件指针。使用新的内部作用域file1
不会指向任何文件,因此写入该文件不会在“beef.txt”
中给出任何结果
移除它以使用正确的对象:
if (file1.is_open())
{
cout << "Enter what you would like to be contained in the file" << endl;
cin >> line;
file1 << line << endl;
}
if(file1.is_open())
{
cout线;
文件1Astd::of Stream
仅用于输出,不能用它读取输入
std::ifstream
仅用于输入,不能使用它写入输出
所以,你也需要
- 使用单独的
std::ofstream
和std::ifstream
变量:
int main()
{
ofstream out_file;
ifstream in_file;
string line;
out_file.open("beef.txt", ios_base::trunc);
if (out_file.is_open())
{
out_file << "beef" << endl;
out_file.close();
}
in_file.open("beef.txt");
if (in_file.is_open())
{
getline(in_file, line);
in_file.close();
cout << "File contains:" << endl;
cout << line << endl;
}
cout << "Enter what you would like to be contained in the file" << endl;
getline(cin, line);
out_file.open("beef.txt", ios_base::trunc);
if (out_file.is_open())
{
out_file << line << endl;
out_file.close();
}
in_file.open("beef.txt");
if (in_file.is_open())
{
getline(in_file, line);
in_file.close();
cout << "File now contains:" << endl;
cout << line << endl;
}
return 0;
}
流文件1;
不要这样做。你需要一个好的循环。@SombreroChicken你可能应该提到哪一个可以通过逐步调试轻松调试此代码。只需使用你的调试器,一行一行地检查每一条语句后磁盘上有什么。我相信这比我们在这里简单地写什么更有帮助t错了。我想他根本不想看文件。只是用错误的英语来表达写作,说“读入文件”。@AlKepp:这个问题还加了[ifstream]
,OP在评论中提到,他也试图使用ifstream
来显示文件,因此我在回答中包括了ifstream
的使用谢谢,字符串现在保存到文件中,但输出是个问题。我使用ifstream来显示文件,但仍然是空白的。(请原谅我的新手)@EthanWeller:请编辑您的问题,以显示您如何尝试使用ifstream
。或者无法打开文件,或者您没有检查该文件,或者您没有正确读取该文件。
if (file1.is_open())
{
cout << "Enter what you would like to be contained in the file" << endl;
cin >> line;
file1 << line << endl;
}
int main()
{
ofstream out_file;
ifstream in_file;
string line;
out_file.open("beef.txt", ios_base::trunc);
if (out_file.is_open())
{
out_file << "beef" << endl;
out_file.close();
}
in_file.open("beef.txt");
if (in_file.is_open())
{
getline(in_file, line);
in_file.close();
cout << "File contains:" << endl;
cout << line << endl;
}
cout << "Enter what you would like to be contained in the file" << endl;
getline(cin, line);
out_file.open("beef.txt", ios_base::trunc);
if (out_file.is_open())
{
out_file << line << endl;
out_file.close();
}
in_file.open("beef.txt");
if (in_file.is_open())
{
getline(in_file, line);
in_file.close();
cout << "File now contains:" << endl;
cout << line << endl;
}
return 0;
}
int main()
{
fstream file;
string line;
file.open("beef.txt", ios_base::out | ios_base::trunc);
if (file.is_open())
{
file << "beef" << endl;
file.close();
}
file.open("beef.txt", ios_base::in);
if (file.is_open())
{
getline(file, line);
file.close();
cout << "File contains:" << endl;
cout << line << endl;
}
cout << "Enter what you would like to be contained in the file" << endl;
getline(cin, line);
file.open("beef.txt", ios_base::out | ios_base::trunc);
if (file.is_open())
{
file << line << endl;
file.close();
}
file.open("beef.txt", ios_base::in);
if (in_file.is_open())
{
getline(in_file, line);
in_file.close();
cout << "File now contains:" << endl;
cout << line << endl;
}
return 0;
}