-4

Not sure if my question is possible in C.

My dilemma is thay the way Linux saves files, a file called input.txt is different to a file called input.

If I want to open input.txt, I would explicitly have to say fopen("input.txt").

Is there a way such that if I want to open a file, I can omit the extension and still expect it to open the file by relying only on the file namd? E. G. fopen("input") would open a file with name "input" save with any extension, such as input.rtf or input.docx?

Thanks.

  • 1
    What if **both** input.rtf and input.docx exist? – Oliver Charlesworth Oct 04 '15 at 20:40
  • You have to implement this manually: first try to find the file using the search condition ( http://stackoverflow.com/questions/3365182/how-to-search-the-computer-for-files-and-folders ) then open the file. – Andrey Nasonov Oct 04 '15 at 20:41
  • 1
    No it's not possible with `fopen` or any other standard library function (AFAIK). And it wouldn't really make a lot of sense. What if there is more than one file that matches? But you could define your own suffix rules and write code to have whatever behaviour you want. – kaylum Oct 04 '15 at 20:41
  • Why bother providing a file name at all? Just think on the file you want to open and hope the code will read your mind. – axiac Oct 04 '15 at 20:44
  • 3
    Note: there is no concept of *extension* in C. The filename is just a string; the semantics are all imposed by the host OS. – wildplasser Oct 04 '15 at 20:46
  • Just to confirm - I agree with what you lot are saying about conflicts. I have coursework that specifies files should be read without providing an input name. I assume when testing the code they won't have conflicting names. –  Oct 04 '15 at 20:49
  • "a file called input.txt is different to a file called input" No, it is not. Linux does not have a concept of file extensions. This is actually a CP/M legacy. It has become common in Linux systems, but you can rename the file to `.exe` and it should still be opend as text file by your editior. – too honest for this site Oct 04 '15 at 20:55
  • @Olaf so for example I can name a .txt file input.dat? If I understood you correctly. Normally doing this in Windows would throw an error (I'm not entirely used to working on Linux yet) –  Oct 04 '15 at 21:05
  • 2
    @F.Tahir yes you can, (in Windows as well, though it will throw a warning that the file may become un-openable). To the computer the file is just a bunch of bytes, and the name a bunch of other bytes. In Windows (and some Linux file managers) the extension is used to determine what program to open the file with, but it doesn't change the content of the file. Hence you can rename a .txt to a .dat, but Windows will no longer open the file with Notepad, unless you explicitly tell it to. – Kninnug Oct 04 '15 at 21:13
  • Okay. That makes sense what this coursework is asking for now. Thanks a lot. –  Oct 04 '15 at 21:18
  • The KDE and Gnome desktops use the extension as a hint, but the shell, etc. does not care about this and your code has no idea either (others already explained that). Without an extension, IIRC, KDE at least (I do not use Gnome) determines the type from the contents, not the name. This is much more reliable. – too honest for this site Oct 04 '15 at 21:20
  • In the next version of C, there will be a function that opens the file by giving only the length of its name. Without the extension, of course – A.S.H Oct 04 '15 at 21:22
  • @A.S.H: Too bad you still have to open it. Python already provides a function which does what you want with the file without even mentioning it. – too honest for this site Oct 04 '15 at 21:30
  • @Olaf, in C too, fclose works perfectly without any name whatsoever ;) – A.S.H Oct 04 '15 at 21:31
  • 1
    @A.S.H: Nope! I just tried passing a duck: Did not quack! – too honest for this site Oct 04 '15 at 21:33
  • Just to confirm if I have a file named input.dat and it's a .txt format (this is possible right), I will have to specify input.dat in my fopen argument and not input.dat.txt? –  Oct 04 '15 at 21:37

2 Answers2

1

Your 'dilemma' is true for almost all filesystems, however the best way to do this would be to loop through all the files in the directory and use strcmp on a substring of the file name.

  • I have this condition, whenever somebody says "substring", I can't help think of `strstr()`. I never think of `strcmp()` though... – EOF Oct 04 '15 at 20:53
  • What function would be suitable for checking substrings? I've yet to come across one in C but saying that I've only been coding in C for a day or two. –  Oct 04 '15 at 21:13
  • Well you could use use `strlen` for the length of the filename you want and the file you're looking, make sure the filename you want is longer, then use `strncmp` – John Schmidt Oct 04 '15 at 21:16
  • There's no substring function in C like there is in Java right? –  Oct 04 '15 at 21:23
  • There is, but you don't need it for this. You use `memcpy` to copy strings. – John Schmidt Oct 04 '15 at 21:28
0

And if you had several files with different extensions (say, input.txt and input.dat), which one would you pick?

You can get a list of all matching files using the glob() function, which is standard in Linux.

Hellmar Becker
  • 2,824
  • 12
  • 18
  • I should have mentioned this was exactly my concern. My coursework requires me to read input files with or without specifying an extension name, so I assume the automated testing will ensure there are no file name conflicts. Otherwise it really doesn't make sense. –  Oct 04 '15 at 20:50
  • But why does fclose() work, without ever specifying anything, neither name nor extension! – A.S.H Oct 04 '15 at 21:30