简体   繁体   中英

In C, how do you open a file without specifying extension?

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? EG fopen("input") would open a file with name "input" save with any extension, such as input.rtf or input.docx?

Thanks.

您的“困境”几乎适用于所有文件系统,但最好的方法是遍历目录中的所有文件并在文件名的子字符串上使用strcmp

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM