#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line = { " " };
ofstream file1;
file1.open("beef.txt");
file1 << "beef" << endl;
file1.close();
file1.open("beef.txt");
if (file1.is_open())
{
cout << "Enter what you would like to be contained in the file" << endl;
cin >> line;
ofstream file1;
file1 << line << endl;
}
return 0;
}
在这个片段中:
if (file1.is_open())
{
cout << "Enter what you would like to be contained in the file" << endl;
cin >> line;
ofstream file1;
file1 << line << endl;
}
您创建另一个ofstream
对象:
ofstream file1;
这会影响您之前创建的那个。而且,你现在正在影子的那个是包含一个指向"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;
}
std::ofstream
仅用于输出,您无法使用它读取输入。
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;
}
std::fstream
变量,可以用于输出和输入,具体取决于在调用in
时是否指定out
和/或open()
标志:
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;
}
std::ofstream
仅用于输出,您无法使用它读取输入。
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;
}