c - 为什么 fread() 在 Msys/MinGw 下不能工作(跳过字节)?

标签 c windows mingw fread xuggle

尝试在 Windows 下构建 Xuggler。 Xuggler 是封装到 Java 中的核心 native 代码函数,用于声音处理目的(包括 ffmpeg)。

我的Windows是x64 Win 7 prof,但所有使用的库都是32位的。我正在 MinGW/MSys 下运行构建过程,从 Msys shell 下使用以下脚本:

#!/bin/sh
export JAVA_HOME=/C/Program\ Files\ \(x86\)/Java/jdk1.6.0_25
export XUGGLE_HOME=/C/Xuggler

PATH=$XUGGLE_HOME/bin:/C/Program\ Files\ \(x86\)/Java/jdk1.6.0_25/bin:/d/APPS/msysgit/msysgit/bin/git:/D/APPS/MinGW/bin:/bin:/D/APPS/apa    che-ant-1.8.2/bin:/D/Users/Dims/Design/MinGW/Util:$PATH
ant -Dbuild.m64=no run-tests

Ant 目标最后包含一些测试,这会给出错误。错误如下

 [exec] Running 6 tests..
 [exec] In StdioURLProtocolHandlerTest::testRead:
 [exec] ../../../../../../../../../test/csrc/com/xuggle/xuggler/io/StdioURLProtocolHandlerTest.cpp:108: Error: Expected (4546420 == totalBytes), found (4546420 != 1042)
 [exec] In StdioURLProtocolHandlerTest::testReadWrite:
 [exec] ../../../../../../../../../test/csrc/com/xuggle/xuggler/io/StdioURLProtocolHandlerTest.cpp:185: Error: Expected (4546420 == totalBytes), found (4546420 != 1042)
 [exec] In StdioURLProtocolHandlerTest::testSeek:
 [exec] ../../../../../../../../../test/csrc/com/xuggle/xuggler/io/StdioURLProtocolHandlerTest.cpp:139: Error: Expected (4546420 == totalBytes), found (4546420 != 1042)
 [exec] .
 [exec] Failed 3 of 6 tests
 [exec] Success rate: 50%
 [exec] FAIL: xugglerioTestStdioURLProtocolHandler.exe

更新1

测试代码如下:

int32_t totalBytes = 0;
do {
    unsigned char buf[2048];
    retval = handler->url_read(buf, (int)sizeof(buf));
    if (retval > 0)
         totalBytes+= retval;
} while (retval > 0);
VS_TUT_ENSURE_EQUALS("", 4546420, totalBytes);

url_read代码如下:

int
StdioURLProtocolHandler :: url_read(unsigned char* buf, int size)
{
    if (!mFile)
        return -1;
    return (int) fread(buf, 1, size, mFile);
}

我不明白,什么情况下可以返回1042???可能是 64 位在这里播放?

更新2

我打印出使用的文件名,它是

d:/......./../../../test/fixtures/testfile.flv

路径正确,但以 d:/ 开头,而不是 /d/

这个在Msys下能发挥作用吗?

更新3

我将读取的字节与测试文件的实际内容进行了比较,发现 fread() 由于某种原因跳过了一些字节。还不知道哪些字节,可能是 CR/LF

更新4

我猜与 CR/LF 无关。

原始字节为

46 4C 56 01 05 00 00 00 09 00 00 00 00 12 00 00 F4 00 00 00 00 00 00 00 02 00 0A 6F 6E 4D 65 74 61 44 61 74 61 08 00 00 ...

读取的字节数为

46 4C 56 15 00 09 00 00 12 00 F4 00 00 00 02 0A 6F 6E 4D 65 74 61 44 61 74 61 80 00 B0 86 47 57 26 17 46 96 F6 E0 40 62 ...

这是 FLV 文件的开始。我不明白腐败的原理。

如何将 01 05 00 00 转换为 15???

更新5

文件打开如下完成

void
StdioURLProtocolHandlerTest :: testRead()
{
  StdioURLProtocolManager::registerProtocol("test");
  URLProtocolHandler* handler = StdioURLProtocolManager::findHandler("test:foo", 0,0);
  VS_TUT_ENSURE("", handler);

  int retval = 0;
  retval = handler->url_open(mSampleFile, URLProtocolHandler::URL_RDONLY_MODE);
  VS_TUT_ENSURE("", retval >= 0);

  int32_t totalBytes = 0;
  printf("Bytes:\n");
  do {
     //...

url_open()函数如下:

int StdioURLProtocolHandler :: url_open(const char *url, int flags)
{
  if (!url || !*url)
    return -1;
  reset();
  const char * mode;

  switch(flags) {
    case URLProtocolHandler::URL_RDONLY_MODE:
      mode="r";
      break;
    case URLProtocolHandler::URL_WRONLY_MODE:
      mode="w";
      break;
    case URLProtocolHandler::URL_RDWR_MODE:
          mode="r+";
          break;
        default:
      return -1;
  }

  // The URL MAY contain a protocol string.  Find it now.
  char proto[256];
  const char* protocol = URLProtocolManager::parseProtocol(proto, sizeof(proto), url);
  if (protocol)
  {
    size_t protoLen = strlen(protocol);
    // skip past it
    url = url + protoLen;
    if (*url == ':' || *url == ',')
      ++url;
  }
//  fprintf(stderr, "protocol: %s; url: %s; mode: %s\n", protocol, url, mode);
  mFile = fopen(url, mode);
  if (!mFile)
    return -1;
  return 0;
}

最佳答案

从今天开始应该在 cross_compile 分支上的 GIT 存储库中修复。我将在本周晚些时候/下周初将其卷入树尖。

现在 stdio 处理程序将所有文件作为二进制文件打开。

关于c - 为什么 fread() 在 Msys/MinGw 下不能工作(跳过字节)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9864606/

相关文章:

windows - 无法在 visual studio 中分析

c - 存储大随机数的最佳哈希函数是什么?

c - C中使用malloc动态分配内存

windows - 在 WIndows 上获取 64 位进程的 TEB

windows - 使用输出运算符在 powershell 中命名 .txt 文件

c++ - mingw windows找不到Eigen头文件

gcc - C 编译器无法创建可执行文件 - Cygwin/MinGW

c++ - 在一个系统中关联另一个 make 实用程序时出错

c++ - 无溢出的 32 位整数缩放

c - 以最有效的方式读取 CGI POST 数据