English 中文(简体)
在工作名录之外C++开设档案
原标题:Opening a file in C++ outside of the working directory
  • 时间:2012-04-10 17:55:15
  •  标签:
  • c++
  • file-io

我推出一个方案,将拥有几个资源档案,用户可以把某些地方放在计算机上,该计算机的号码与可起诉的相同。 我如何打开这些档案?

我找到了许多答案,说为什么事情没有奏效,就是说档案在工作目录中是一纸空文。 我尝试提供完全合格的途径:

ifstream str;
str.open("/home/millere/foo.txt")

但未成功。 我知道这条道路是正确的(范围和过去)。 我无法找到这方面的任何文件,但我认为有必要这样做。 (vim ~/foo.txt 例如,从<代码>~以外的任何地方提取。

最佳回答

Assuming you meant to use ifstream instead of iostream, your code is correct. ifstream can use a path to a file as well as the name of a file in the working directory.

No exceptions are thrown if the file does not exist, but the fail bit is set. You should check for this before trying to do anything with the stream.

std::ifstream input("/home/bob/stuff.txt");

if (!input) std::cerr << "Could not open the file!" << std::endl;

else
{
    // ...
}

如果你仍然无法从档案中提取数据,问题在你的法典中是其他地方。

问题回答

I had the same issue and quickly noticed that, open when trying to get from a difference folder, had a different source directory (if using Cmake, the one that was specified by the cmake). You can find out, what the ouput/input source directory is by doing

system("ls")

system("dir")

显示现有数据/投入目录内容的窗口。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签