c++ - 通过 Visual Studio 运行程序导致它失败?

标签 c++ visual-studio-2010 bittorrent

我正在编写的程序有问题。它是一个解析 bencode(用于 torrent 文件)的命令行解析器。该程序接受文件名作为命令行。当我使用调试命令行参数设置在 Microsoft Visual Studio 10.0 中构建和运行程序以输入命令行时,程序告诉我它解析失败。

如果我打开命令提示符并使用相同的命令行从命令提示符运行程序,程序将完美运行!这是怎么回事?这是 Visual Studio 的常见问题吗?

我使用 Visual Studio 中的调试器来跟踪程序失败的位置,似乎对用于获取文件长度的统计函数 (http://msdn.microsoft.com/en-us/library/14h5k7ff.aspx) 的调用在 Visual Studio 中返回错误,但在运行时工作正常在 Visual Studio 之外。

该代码使用 Bencode 解析器,可在此处找到:http://funzix.git.sourceforge.net/git/gitweb.cgi?p=funzix/funzix;a=blob;f=bencode/bencode.c

程序代码如下:

#include <stdio.h>
#include "../Parse/bencode.h"

int main(int argc, char *argv[]){
if(argc != 2){
    printf("Usage: whirlwind filename\n");
    return 1;
}

char *buf;
long long len;
be_node *n;

//read the torrent file into a buffer and store at &buf
buf = read_file(argv[1], &len);
if(!buf){
    buf = argv[1];
    len = strlen(argv[1]);
}

printf("Decoding: %s\n", argv[1]);
n = be_decoden(buf, len);

if(!n){
    printf("Parsing failed!\n");
    return 1;
}

if(n->type != BE_DICT){
    printf("This file is not a valid Bencoded Dictionary.\n");
    return 1;
}

int i;
char* keyName;

for(i = 0; i < 10; i++){
    keyName = n->val.d[i].key;
    if(keyName == "announce"){
        printf("\n\n");
    }
    printf("%s\n", keyName);
    if(keyName == "announce"){
        printf("\n\n");
    }
}

return 0;

最佳答案

如果您从 Visual Studio 传递相对路径,您应该确保当您的应用程序在 IDE 中运行时它能够正确解析。出现这个问题是因为在调试的时候,当前目录一般是\bin\debug。
为了安全起见,请输入完整路径名或从配置文件中读取文件的位置。

关于c++ - 通过 Visual Studio 运行程序导致它失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11492169/

相关文章:

c# - 字符串参数太长。从数据库中检索数据到word模板

visual-studio-2010 - 尽管字符集正确,德语元音仍显示错误

c# - Visual Studio Extensibility Package 2010 和 2012。有没有办法同时拥有一个包?

p2p - 如何在 Bittorrent DHT 中找到具有精确 info_hash 的节点?

c++ - 如何解决 "Library not loaded:"问题?

c++ - 如何将一行中的前两个字符串写入各自的变量,并将该行的其余内容写入其自己的数组?

c++ - `const` 对象的构造函数

java - 为 torrent 计算的 info_hash 不正确

php - 如何转换 torrent 信息哈希以进行抓取?

c++ - GDI+ - 将位图像素转换为 JPG 格式