如何找到的文件的大小在不同的相对使用目录统计()中C 2 [英] How to find the size of a file in a different relative directory using stat() in C?

查看:185
本文介绍了如何找到的文件的大小在不同的相对使用目录统计()中C 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的函数中使用统计()来获取文件的大小:

Here's my function to get the size of a file using stat():

off_t fsize(const char *filename) {                                                                                                                   
  struct stat st;                                                                                                                                        

  if (stat(filename, &st) == 0)                                                                                                                          
     return st.st_size;                                                                                                                                 

  fprintf(stderr, "Cannot determine size of %s: %s\n",                                                                                                   
         filename, strerror(errno));                                                                                                                    

  return -1;                                                                                                                                             
}

我可以得到文件的大小只是做精:

I can get the file size just fine by doing:

size = fsize ("byte.bin");

但是当我需要从本地目录的下级目录获取文件,让我们说DEPS / src目录目录下,它pmaturely停止$ P $在我身上,并没有错误消息,我预计:

but when I need to get the file from a lower directory from the local directory, let's say "deps/src" directory, it stops prematurely on me with no error message that i expected:

size = fsize ("deps/src/byte.bin");

我写一个使用功能的小程序,复制byte.bin文件到DEPS / byte.bin,并要求我的fsize功能的DEPS / byte.bin,并得到错误无法确定大小byte.bin:没有这样的文件或目录

I wrote a small program that uses the function, copied the byte.bin file to a "deps/byte.bin" and called my fsize function with "deps/byte.bin" and get the error "Cannot determine size of byte.bin: No such file or directory"

如果我用像/something/deps/byte.bin绝对路径它的工作原理。

If I use an absolute path like "/something/deps/byte.bin" it works.

我是什么做错了,以及如何显示我的相对路径做到这一点?

What am I doing wrong and how show I do this for the relative path?

推荐答案

我猜发生了什么事是你错误地认为有关的工作目录。测试一个简单的方法在哪里工作目录是只写一个程序,只是输出如的test.txt 的文件。在许多(但不是全部)的情况下,工作目录是哪里的可执行文件被存储。这意味着,如果你试图访问一个相对路径上的一个文件,你可能需要包括至少一个 .. 在你的相对路径走出的目录

I'm guessing that what's happening is that you are incorrectly assuming about the working directory. A simple way to test where the working directory is would be to just write a program that simply outputs a file like test.txt. In many (but not all) cases, the working directory is wherever the executable file is stored. This means that if you are trying to access a file on a relative path, you will likely need to include at least one .. in your relative path to get out of the bin directory.

这篇关于如何找到的文件的大小在不同的相对使用目录统计()中C 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆