我正在编写的程序有问题。它是一个解析 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/