简体   繁体   English

来自c ++中其他目录中文件的ifstream对象

[英]ifstream objects from files in other directories in c++

I have saved all of the files that I intend to use as input streams in my application in a folder called "res". 我已将打算用作输入流的所有文件保存在应用程序中的“ res”文件夹中。 I am trying to use these files as input streams by building the string in the application and then opening them using something like this: 我试图通过在应用程序中构建字符串,然后使用如下所示的方式将它们用作输入流:

....
ifstream inputFile;
....
char fileName[30];
strcpy(fileName,"res/")
switch(num){
case 1:
    strcat(fileName,"file1");
case 2:
    strcat(fileName,"file2");
....
}

inputFile.open(fileName);
....

But the files are not opening and I don't know why; 但是文件没有打开,我也不知道为什么。 the inputFile.is_open() method is returning false. inputFile.is_open()方法返回false。 So I have 2 questions: 所以我有两个问题:

  1. How do I refer to files in sub-directories and, for that matter, files in other folders in the parent directory? 如何引用子目录中的文件,以及在父目录中其他文件夹中的文件?

  2. Is there a better practice than the one I am using for doing what I'm trying to do above?? 是否有比我上面做的更好的练习? I am pretty sure there is! 我很确定有!

I am using eclipse CDT on Ubuntu 12.04, and the g++-4.6 compiler, if that makes a difference. 我在Ubuntu 12.04和g ++-4.6编译器上使用eclipse CDT,如果有区别的话。

actually, right answer is given by david in comment. 实际上,大卫在评论中给出了正确的答案。 you forgot breaks after cases and concatenate multiple names. 您忘记了在个案之后的休息时间,并串联了多个名称。 also with your mad skillz you better avoid strcat and other low-level functions and use std::string, or your next question will be about your program dumping core 还要借助疯狂的Skillz,您最好避免使用strcat和其他低级函数,并使用std :: string,否则您的下一个问题将与程序转储核心有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM