Using ifstream, ofstream and fstream
Using ifstream, ofstream and fstream
-
试图读取一个单词"牛肉"
-
后来进行,然后将文件内容编辑给用户想要的任何内容,这些内容将存储在字符串中," line"
-
该文件永远不会显示,当我手动检查它时,文本文件为空白。
#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"
的对象。使用新的Inter-Scope 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
变量,可用于输出和输入,具体取决于您在调用open()
时是否指定in
和/或out
标志: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; }
相关文章:
- 在fstream期间忽略空行
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 为什么 Clang 不允许"and"作为函数名称?
- 位阵列上的快速AND运算
- 是否可以在 C++03 中定义'move-and-swap idiom'等效项
- 使用fstream对txt文件中的数字进行循环
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 无法打开 fstream C++文件,即使它与 .cpp 位于同一位置
- BoostPython and CMake
- OpenSSL BIO and SSL_read
- 使 std::fstream 写入文件末尾,但从头开始读取
- Gurobi GRBModel and GRBmodel in C++
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- std::visit and std::variant usage
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- Using ifstream, ofstream and fstream
- fstream !fail() and is_open()
- fstream and ofstream
- c++ fstream and seekp