打开错误文件的错误消息

发布于 2024-11-09 03:40:55 字数 111 浏览 0 评论 0原文

我正在使用 qt 开发 C++ 代码的接口,所以我想知道如何检查用户从打开的文件打开它的文件类型,因为程序只接受视频文件。如果文件是除视频文件之外的任何文件,我想显示错误消息。

提前致谢 :)

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 message if a file was anything except video files.

Thanks in Advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奈何桥上唱咆哮 2024-11-16 03:40:55

文件“类型”只是一种简化。在现代操作系统上,从操作系统的角度来看,所有磁盘文件都是相同的(它们只是字节序列),文件的“类型”仅取决于读/写这些文件的程序。过去,许多操作系统用于区分二进制文件和文本文件,但这种时代几乎完全消失了。

使文件类型“明显”的常见方法是使用命名约定...即名称以“.mpg”结尾的文件可能是使用 MPEG 标准或“.txt”编码的视频文件文件可能包含人类可读的文本。

因此,您有两个选择...要么您只根据文件名过滤文件列表(例如,您只接受以“.mp4”、“.mpg”、“.mpeg”、“.avi”、“. wmv”、“.webm”、“.ogg”... - 仅举几个常见的视频文件扩展名)或者您只是尝试将任何文件传递给程序,程序将决定是否可以播放它。如果程序是个好程序,如果它无法理解特定的文件格式,它会优雅地失败而不是崩溃。

请注意,即使您使用 Qt(便携式库),如果可接受的视频文件编码取决于操作系统甚至单个安装,我也不会感到惊讶(因为视频/编解码器编解码器通常是“系统上可能安装或未安装的插件”)。

如果您确实想限制文件名,请记住添加一种方法来选择扩展名不在列表中的文件。每次你在程序中枚举事物时,你首先都会感到傲慢(列表意味着你知道所有可能的情况,你认为你真的知道吗?)并且你正在为未来的兼容性设置障碍(例如,下一个版本的播放器程序还可以处理 .wzz 视频文件,但您的程序将无法播放它们,因为允许的扩展名列表中有一个愚蠢的限制。

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).

桜花祭 2024-11-16 03:40:55

您确实意识到您已经可以使用 QFileDialog 之类的东西来过滤文件类型,对吧?只需查看文档,尤其是过滤器。有几个例子。

否则只需检查文件扩展名。

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.

一江春梦 2024-11-16 03:40:55

恕我直言,比文件扩展名更可靠的事情是检查文件头。您需要读取文件的第一个字节(标头)并查看它们是否与您的程序支持的视频容器格式之一匹配。

例如,Windows AVI 文件以十六进制字节开头:52 49 46 46(“RIFF”),而 MKV 文件以 1A 45 DF A3 开头(如果我没记错的话)。

当然,这并不能保证您的系统实际上可以播放此类文件(这取决于您是否有正确的编解码器/过滤器以及您的文件是否未损坏),但它比检查扩展名更可靠,扩展名只是任意的名称并不能真正告诉您有关文件中实际内容的任何信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文