使用ifstream,ofstream和fstream

问题描述 投票:-3回答:3
  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;
}
c++ fstream ifstream ofstream
3个回答
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;
}

您创建另一个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;
}

0
投票

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变量,可以用于输出和输入,具体取决于在调用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; }

-1
投票
  • 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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.