Using ifstream, ofstream and fstream

Using ifstream, ofstream and fstream

本文关键字:and fstream ofstream ifstream Using      更新时间:2023-10-16
  1. 试图读取一个单词"牛肉"

  2. 后来进行,然后将文件内容编辑给用户想要的任何内容,这些内容将存储在字符串中," line"

  3. 该文件永远不会显示,当我手动检查它时,文本文件为空白。

#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::ofstreamstd::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;
    }