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

Having trouble processing a file from my documents in my code?

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

我只是想在文件处理和 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 文件,但我在正确访问其他目录时遇到问题?

有什么想法吗?

谢谢!

您的代码完全有效并且有效 - 我在行

中用我自己的文件而不是您的文件试过了
    string filepath = "C:/Users/David Laptop/Documents/my games/oblivion/RenderInfo.txt";

所以你没有这样的文件它不在给定的path 或者你没有这样的path.

在那一行改正就OK了。

Tip: Find your file in Windows Explorer, press (and keep pressing) Shift) and right-click on this file. From the context menu then choose Copy as path and then paste it to your code. But be carefull - you have to change every backslash (\) to a forward slash (/) (as in your code) or use double backslashes (\) instead of a single one.