在红宝石......
我有一个由外部进程创建的IO对象,我需要从中获取文件名.但是我似乎只能得到文件描述符(3),这对我来说不是很有用.
有没有办法从这个对象获取文件名甚至获取文件对象?
我从通知程序获取IO对象.所以这可能是一种获取文件路径的方法?
我见过的所有内容都说要使用lsof -p,但我正在寻找一些不需要fork/exec的东西.
例如在Linux上,人们可以简单地走路/proc/{pid}/fd.
我注意到OS X和Linux上都有一些意外的行为.打开O_NONBLOCK标准输出的非阻塞I/O(使用)也可以打开标准输入!
这些操作系统是否正常运行?如果是这样,这个行为是否由POSIX定义?如果是这种情况,请指出相关文件.
这是我用来测试这个的示例程序:
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char *argv[]) {
int flags = fcntl(STDOUT_FILENO, F_GETFL);
if (argc > 1 && strcmp(argv[1], "1") == 0) {
fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);
}
printf("stdout O_NONBLOCK is: %d\n", fcntl(STDOUT_FILENO, F_GETFL) & O_NONBLOCK);
printf("stdin O_NONBLOCK is: %d\n", fcntl(STDIN_FILENO, F_GETFL) & O_NONBLOCK);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在OS X上:
$ clang -o fd-test fd-test.c
$ ./fd-test
stdout O_NONBLOCK is: 0
stdin O_NONBLOCK is: 0
$ ./fd-test …Run Code Online (Sandbox Code Playgroud) 在我的类中,我有FILE* ascii_file;数据成员,它总是通过类构造函数初始化.我怎样才能获得完整的路径asci_file?我不想存储有关该文件的更多信息ascii_file,并希望它可以在windows,linux和solaris上运行.
有什么办法可以从C中的文件指针中找到file_name?
fp = fopen(file,"r");
Run Code Online (Sandbox Code Playgroud)
从fp,可以获取我打开的文件名吗?
在 linux 内核模块中,有没有办法从unsigned int fd?
我知道这个答案:如何从内核模块内的文件描述符获取文件名?但如果我理解正确的代码,我也需要一个struct files_struct。
编辑:
请停止投票,因为它不是重复的。我正在寻求一种从内核模块以纯 C 格式获取文件名/路径的方法,而不是使用系统工具。换句话说:在 /procself/fd/ 上运行 readlink不是一个好的答案。
编辑2:
内核的系统调用读取ssize_t read(int fd, void *buf, size_t count);需要 3 个参数,其中一个是 fd。很明显,以某种方式read能够读取单个文件(而不是 inode 中的所有文件)。问题是如何。
当用户点击谷歌硬盘中的"发送文件"按钮并选择我的应用程序.我想获取该文件的文件路径,然后允许用户将其上传到其他位置.
我为kitkat手机检查了这些类似的SO帖子:从URI,Android KitKat新的存储访问框架获取真实路径
然而,解决方案似乎不再适用于Lollipop设备.
问题似乎是MediaStore.MediaColumns.DATA在ContentResolver上运行查询时返回null.
https://code.google.com/p/android/issues/detail?id=63651
您应该使用ContentResolver.openFileDescriptor()而不是尝试获取原始文件系统路径."_data"列不是CATEGORY_OPENABLE合同的一部分,因此不需要Drive返回它.
我已经阅读了CommonsWare的这篇博文,其中建议我"尝试直接使用Uri直接使用ContentResolver",这是我不明白的.如何直接在ContentResolvers中使用URI?
但是,我仍然不清楚如何最好地处理这些类型的URI.
我能找到的最佳解决方案是调用openFileDescriptor,然后将文件流复制到一个新文件中,然后将该新文件路径传递给我的上传活动.
private static String getDriveFileAbsolutePath(Activity context, Uri uri) {
if (uri == null) return null;
ContentResolver resolver = context.getContentResolver();
FileInputStream input = null;
FileOutputStream output = null;
String outputFilePath = new File(context.getCacheDir(), fileName).getAbsolutePath();
try {
ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
FileDescriptor fd = pfd.getFileDescriptor();
input = new FileInputStream(fd);
output = new FileOutputStream(outputFilePath);
int read = 0;
byte[] bytes = new byte[4096];
while ((read …Run Code Online (Sandbox Code Playgroud) android android-intent google-drive-api storage-access-framework
嗨,任何人都可以告诉我c中isatty()的参数是什么.我有以下代码,但我不明白第一个输出三个数字是1,所有左边是0.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
for(int i=0;i<100;i++){
int t=isatty(i);
printf("%d",t);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个问题fts(3).每当我尝试访问该fts_children()函数的任何成员时,我都会遇到分段错误.当我阅读http://www.kernel.org/doc/man-pages/online/pages/man3/fts.3.html上的手册页时,它声称在读取函数运行后填充自己并返回链接的链接列表通过link结构领域.我的怀疑是child_function没有返回任何内容,但我觉得这不符合手册页.我应该将这些文件添加到子缓冲区,因为我认为这是自动完成的吗?我的代码在下面,谢谢!
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fts.h>
#include<string.h>
int compare (const FTSENT**, const FTSENT**);
int main(int argc, char* const argv[])
{
FTS* file_system = NULL;
FTSENT* child = NULL;
FTSENT* parent = NULL;
FTSENT* temp = NULL;
file_system = fts_open(argv + 1,FTS_COMFOLLOW | FTS_NOCHDIR,&compare);
while( (parent = fts_read(file_system)) != NULL)
{
child = fts_children(file_system,0);
printf("%s\n", child->fts_path);
}
// while (child ->fts_link != NULL)
// child = child->fts_link;
fts_close(file_system);
return 0;
}
int compare(const …Run Code Online (Sandbox Code Playgroud) 如何在C++代码中从shell重定向获取文件名?ie ./MyProgram <myfile,我想逐行获取myfile作为文件名及其内容.
**编辑:我设法从文件中获取输入.谢谢您的帮助.但是,在循环遍历文件内容之后,我想用cin保持用户输入.就像这样:
while (true)
{
if (cin.eof() == false)
{
getline(cin, line);
cout << line;
}else{
cin >> choice;
}
}
Run Code Online (Sandbox Code Playgroud)