[英]C - How do I make a function that finds the location of a file it has to use just by giving it the filename? (Windows)
I am having trouble with the function fopen()
.我在使用
fopen()
函数时遇到问题。 I would always send the exact location of a file as an arguement to fopen()
, which would look something like this:我总是将文件的确切位置作为参数发送给
fopen()
,它看起来像这样:
fopen("c:\\\\Users/Username/Desktop/Projects/program_name/version 1.0/data/important_data.txt", "r");
That works just fine on my computer, but what if I decide to transfer the program to another computer?这在我的电脑上运行得很好,但是如果我决定将程序转移到另一台电脑上怎么办? The location would completely change.
位置将完全改变。
It would no longer be:它将不再是:
c:\\\\Users/Username/Desktop/Projects/program_name/version 1.0/data/important_data.txt
But it would rather be something like:但它宁愿是这样的:
c:\\\\Users/OtherUsername/Desktop/program_name/version 1.0/data/important_data.txt
My question is, how do I make a portable function which can obtain the location of a file, if I only give the name (including the type eg .txt) of the file to the function?我的问题是,如果我只将文件的名称(包括类型,例如 .txt)提供给函数,我该如何制作一个可以获得文件位置的可移植函数?
Keep in mind, I've been learning C for less than a year.请记住,我学习 C 语言还不到一年。 There are still a lot of things which I must learn, and things like this are of high importance.
我还有很多东西需要学习,像这样的东西很重要。
this is operating system specific.这是操作系统特定的。 on linux you can use the
locate
shell command and parse its output ( http://www.linfo.org/locate.html )在 linux 上,您可以使用
locate
shell 命令并解析其输出( http://www.linfo.org/locate.html )
C: Run a System Command and Get Output? C:运行系统命令并获取输出?
How do I execute a Shell built-in command with a C function? 如何使用 C 函数执行 Shell 内置命令?
however this solution will only work on linux.但是,此解决方案仅适用于 linux。 i think yano 's solution in the comments above is better ...
我认为yano在上面评论中的解决方案更好......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.