开发者

error message for open wrong file [closed]

开发者 https://www.devze.com 2023-03-07 08:21 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am working on qt to develop interfaces for c++ code,so I want to know how can I check file type that a user opens it from open file because a program accepts only video files. I want to appear error m开发者_StackOverflow中文版essage if a file was anything except video files.

Thanks in Advance :)


File "type" is just a simplification. On modern operating systems all disk files are equal from an OS point of view (they're just sequences of bytes) and the "type" of a file only depends on the program that read/write those files. In the past a lot of operating systems used to discriminate e.g. between binary files and text files but those times are almost completely gone.

A common approach to make the type of a file "evident" is to use a naming convention... i.e. a file with a name ending in ".mpg" is probably a video file encoded using MPEG standard, or a ".txt" file probably contains human-readable text.

Therefore you have two options... either you just filter the file list depending on the file names (e.g. you only accept files ending with ".mp4", ".mpg", ".mpeg", ".avi", ".wmv", ".webm", ".ogg" ... - just to name a few common video files extensions) or you just try to pass any file to the program and the program will decide if it can play it or not. If the program is a good one it will fail gracefully if it can't understand a specific file format instead of just crashing.

Note that even if you're using Qt (a portable library) I'd wouldn't be surprised if what are the accepted video file encodings depends on the operating system and even on the single installation (because video/codecs codecs are often "plugins" that may be or may be not installed on a system).

If you really want to limit the file names then please remember to add a way to select a file with an extension that it's not in your list. Every single time you enumerate things in your program you are first of all sort of arrogant (a list implies that you know all possible cases, do you think you really do?) and you are placing an obstacle to future compatibility (e.g. next version of the player program will handle also .wzz video files but your program will not able to play them because of a stupid limitation in the list of allowed extensions).


You do realize you can already filter the file types with something like a QFileDialog, right? Just have a look at the documentation and especially filters. There are several examples.

Otherwise just check for the file extension.


Something a bit more reliable than file extensions imho, would be to check the file header. You need to read the first bytes of the file (the header) and see if they match one of the video container formats your program supports.

For example, a windows AVI file starts with the hex bytes: 52 49 46 46 ("RIFF") while MKV files begin with 1A 45 DF A3 (if I remember correctly).

This of course doesn't guarantee that your system can actually play such files (that depends on wheter you have the proper codec/filter and if your file isn't corrupt) but it's more reliable than checking the extension, which is just an arbitrary name that doesn't really tell you anything about what's actually inside the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号