2016-11-06 63 views
-1

我已经在网络和这里搜索了这个,但我似乎无法弄清楚我做错了什么。在我的代码中处理文件时遇到问题?

我只是想通过文件处理和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?

任何想法?

谢谢!

+0

什么具体不工作? – Carcigenicate

+0

[在工作目录之外的C++中打开文件]可能的重复(http://stackoverflow.com/questions/10093891/opening-a-file-in-c-outside-of-the-working-directory) – eshirima

+0

该文件没有被打开,因此没有被复制到output_file.txt中 –

回答

0

你的代码是完全有效的,它的工作原理 - 我用我自己的文件试了一下,而不是你的,在该行

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

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

纠正它在那一行,它会没事的。

提示:查找Windows资源管理器中,按您的文件(按住)Shift)并在此右键单击文件。从上下文菜单中选择Copy as path,然后将其粘贴到您的代码中。但要小心 - 必须将每个反斜杠(\)更改为正斜杠(/)(如代码中所示)或使用双反斜杠(\\)而不是单个反斜杠。

+0

哇...我很抱歉,该文件是“RendererInfo.txt”而不是“RenderInfo.txt”....我正在拍我的脸,但感谢您的帮助! –

+0

超级!我用一个提示扩展了我的答案,如何*防止类似的错误*(将来)。 – MarianD

+0

非常感谢! –

相关问题