c++ - 从外部创建的文件中读取数据

标签 c++

我正在尝试读取在程序外部创建的文件,但遇到了一些问题。该程序让用户创建一个文件。然后它从程序外部创建的两个 .txt 文件中读取单词,然后将这些单词写入创建的文件。

#include "std_lib_facilities.h"

int main()
{
    string word;

    cout << "Create file.\n";
    char name[20];
    cin >> name;
    ofstream ost(name, ios::out);

    cout << "Open first file.\n";
    char filename[20];
    cin >> filename;      
    ifstream ist(filename);
    while(ist >> word) ost << word << " ";
    ist.close();

    cout << "Open second file.\n";
    cin >> filename;
    ifstream isttwo(filename);
    while(isttwo >> word) ost << word << " ";
    isttwo.close();

    ost.close();

    keep_window_open();
}

但是,当我在记事本中打开创建的文件时,它是空白的。这是因为读取字符串是不可能的,因为正在读取的文件是单独创建的吗?我不太确定。感谢您的帮助。

最佳答案

代码是正确的。只要确保当您写下第一个文件的名称和第二个文件的名称时,您也写下了它们的扩展名。 例如:

first.txt
second.txt

关于c++ - 从外部创建的文件中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1183782/

相关文章:

c++ - 类中的 Typedef 函数

c++ - link.exe 返回错误 LNK1181 : cannot open input file 'C:\Program.obj'

c++ - 将 weak_ptr 与原始指针进行比较不起作用,正在寻找替代方案

用于列表和 map 的 C++ 容器

c++ - 是否可以隐藏模板类的实现?

c++ - 在 C++ 中返回对象

c++ - 推力 CUDA 找到每个组(段)的最大值

c++ - Lambda 和 bind 在 VS2010 中不能一起工作

C++ reinterpret_cast 对 std::shared_ptr 引用进行优化

c++ - 如何仅在邻域中的非常大的图像上计算成对 L1 距离矩阵?