0

I'm trying to write a C code that does stuff with the files. Yet I want to not being have to set the absolute directory in the code, but rather just give the name and leave the hardwork of finding where it is to the computer. Something like this:

filename = searchForDirectory("exampleFile.txt");
fopen(filename, r);

Is something like this possible with C, and if it is, how?

Edit: To clarify, I want to be able to give a file name to -say- a function, which will scan all the directories in the disk, then return the directoy of the first occurance of that file name. I guess find in Linux does something like this. Is there a Windows counterpart?

aulven
  • 521
  • 3
  • 14
  • This is unrelated to the language. But usually, yes it is allowed, the file iss simple beeing looked for in the current directory. I'm not aware of any platform where the concept of "current directory" does not exist. OTOH the file won't be searched for in all directories of the file system, this wouldn't make much sense, would it? Think about it. – Jabberwocky Mar 20 '20 at 15:04
  • 3
    Files with the same name may exist in more than one directory, but not in the same directory. So a file must be fully specified, and that can include a default directory. – Weather Vane Mar 20 '20 at 15:07
  • 3
    It seems to me that you didn't make the right question. Can you be more specific about why you have doubts? – Iharob Al Asimi Mar 20 '20 at 15:09
  • Actually this sounds like an [XY Problem](http://xyproblem.info/). You need to tell us what you're _actually_ trying to achieve? For all clarifications [edit] your question. – Jabberwocky Mar 20 '20 at 15:15
  • The same file name (not the same file) may be in more than one directory ... Yes in C is possible to write a code that searches a file in all system disk(s). (E.G: see the Linux `find` utility) – Sir Jo Black Mar 20 '20 at 16:03
  • Okay, sorry for ambiguous and possibly senseless question. I tried to clarify it a little. – aulven Mar 20 '20 at 16:54
  • @melonduofromage thanks for your edit, but this is a totally different question. You are probably looking for this: https://stackoverflow.com/a/612112/898348 – Jabberwocky Mar 21 '20 at 16:54
  • @Jabberwocky Yeah, apperantly even I wasn't sure what I wanted when asked it. Thanks for the source. – aulven Mar 22 '20 at 14:31

0 Answers0