在我的代码中处理文档中的文件时遇到问题?

时间:2016-11-06 01:15:56

标签: c++ file fstream

我已经在网上和这里搜索了这个,但我似乎无法弄清楚我做错了什么。

我只是想通过文件处理和c ++来做得更好。

对于练习,我试图从游戏文件夹中获取文本文件并复制它。

这是我的代码(无法访问该文件)。

#include <fstream>

#include <iostream>
#include <string>

using namespace std;



int main()

{
    //define / open files
    ifstream my_input_file;
    ofstream my_output_file;
    string filepath = "C:/Users/David Laptop/Documents/my games/oblivion/RenderInfo.txt";
    my_input_file.open(filepath);


    if (my_input_file.is_open())
    {
        cout << "opened\n";
        my_output_file.open("output_file.txt", ofstream::trunc);
        char c;
        my_input_file.get(c);
        while (my_input_file)
        {
            my_output_file.put(c);
            my_input_file.get(c);
        }

        my_input_file.close();
        my_output_file.close();


    }
    else
    {
        cout << "FAIL\n";
    }

    cin.get();
    return 0;
}

在项目目录中,这似乎适用于文本文件和.ini文件,但我遇到了正确访问其他directiorys的问题?

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码完全有效并且有效 - 我在中使用我自己的文件而不是您自己的文件

    string filepath = "C:/Users/David Laptop/Documents/my games/oblivion/RenderInfo.txt";

所以你没有这样的文件它不是在给定的路径中你或没有这样的路径

在该行中更正它,就可以了。

  

提示:在Windows资源管理器中查找文件,按(并按住)Shift)并右键单击此文件。从上下文菜单中选择Copy as path,然后将其粘贴到您的代码中。但请注意 - 您必须将每个反斜杠(\)更改为正斜杠(/)(如在代码中)或使用双反斜杠(\\)而不是单个反斜杠(-- zero-pad to 4 digits: select substr('0000'||3,-4);