为什么Eclipse在我的C命令行参数中添加单引号?

问题描述 投票:5回答:2

我在Windows  10机器上运行Eclipse v4.6(Neon1)RC1,它似乎是在调试期间传递的每个命令行参数/参数周围添加单引号。我看到每个argv都会在内存中显示出来。

奇怪的是我不能用printf在控制台上产生这些引号(在RUN期间);程序成功加载参数指定的文件并输出到控制台。

命令行参数在应用程序运行config-> Parameters选项卡中设置为(单行,不添加引号):

keyFile.txt inputFile.txt outputFile.txt

我没有在NetBeans中看到这种行为(实际上已暂时切换)。

问题是这似乎在调试时导致fopen出现问题:它无法找到该文件。我假设两个都使用相同的工作目录,因为如果我使用静态文件名“keyFile.txt”,调试工作正常。

不幸的是我的控制台输出在调试中不起作用,所以我现在有点受限。

示例代码是一个非常精简的版本,用于演示:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {

    // Arguments: keyfile.txt inputfile.txt outputfile.txt

    char * firstArg = argv[1];
    char * secArg   = argv[2];   // Leaving these to show quotes on other inputs as well
    char * thirdArg = argv[3];

    printf("First arg:\t%s\n", firstArg);
    printf("Ptr Address, uint cast:\t0x%x\n", (unsigned int) firstArg);
    printf("Ptr Address, void* cast:\t%p\n", (void *) firstArg);
    printf("Char at Ptr:\t%c\n\n", (char) *(firstArg));

    printf("Second arg: \t%s\n", secArg);
    printf("Third arg: \t%s\n", thirdArg);

    FILE *fptr;
    fptr = fopen(firstArg, "rb");
    if (fptr == NULL)
     {
        perror("Error");
        return -1;
     }

    int kLength=0;
    int inputChar;
    unsigned char keyin[256];

    printf("\nData from file:\n");
    while ((inputChar = fgetc(fptr))!=EOF)        // Read KEYFILE
    {
        if ((kLength%8)==0) { printf("\n"); }
        keyin[kLength++] = (unsigned char) inputChar;
        printf("0x%x\t",inputChar);
    }

    return 0;
}

在运行时,输出符合预期/如下:

First arg:    keyFile.txt
Ptr Address, uint cast:        0x6b1748
Ptr Address, void* cast:    006B1748
Char at Ptr:    k

Second arg:     inputFile.txt
Third arg:     outputfile.txt
current Path: C:\Users\***\Google Drive\***\eclipse workspace\argTest
Data from file:

0x59    0x45    0xba    0x1e...
...
(data I expect is displayed from file ...)

但是在调试期间,我被一个空的fptr困住了,我在内存中看到了引号:

调试内存:firstArg指向0x6E1760: Debug memory: firstArg points to 0x6E1760

看看gdb跟踪,我也看到了这里的单引号。下面显示的是导致这一点的一切:

311,234 2-gdb-version
311,239 ~"GNU gdb (GDB) 7.6.1\n"
311,240 ~"Copyright (C) 2013 Free Software Foundation, Inc.\n"
311,240 ~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is fre\
e software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitt\
ed by law.  Type \"show copying\"\nand \"show warranty\" for details.\n"
311,240 ~"This GDB was configured as \"mingw32\".\nFor bug reporting instructions, please see:\n"
311,240 ~"<http://www.gnu.org/software/gdb/bugs/>.\n"
311,240 2^done
311,241 (gdb)
311,243 3-environment-cd "C:/Users/***/Google Drive/***/workspace/argTest"\
311,250 3^done
311,250 (gdb)
311,251 4-gdb-set breakpoint pending on
311,260 4^done
311,260 (gdb)
311,261 5-gdb-set detach-on-fork on
311,270 5^done
311,270 (gdb)
311,271 6-enable-pretty-printing
311,280 6^done
311,280 (gdb)
311,281 7-gdb-set python print-stack none
311,290 7^done
311,290 (gdb)
311,291 8-gdb-set print object on
311,300 8^done
311,300 (gdb)
311,301 9-gdb-set print sevenbit-strings on
311,310 9^done
311,310 (gdb)
311,311 10-gdb-set host-charset UTF-8
311,320 10^done
311,320 (gdb)
311,321 11-gdb-set target-charset WINDOWS-1252
311,330 11^done
311,330 (gdb)
311,331 12-gdb-set target-wide-charset UTF-16
311,340 12^done
311,340 (gdb)
311,342 13source .gdbinit
311,350 &"source .gdbinit\n"
311,350 &".gdbinit: No such file or directory.\n"
311,350 13^error,msg=".gdbinit: No such file or directory."
311,350 (gdb)
311,351 14-gdb-set target-async off
311,360 14^done
311,360 (gdb)
311,361 15-gdb-set auto-solib-add on
311,370 15^done
311,370 (gdb)
311,379 16-file-exec-and-symbols --thread-group i1 Debug/argTest.exe
311,384 16^done
311,384 (gdb)
311,385 17-gdb-set --thread-group i1 args 'keyFile.txt' 'inputFile.txt' 'outputFile.txt'
311,394 17^done
311,394 (gdb)
...
c eclipse debugging command-line quotes
2个回答
3
投票

这是Eclipse中的已知错误,在此处进行跟踪:


1
投票

我的解决方法是在.gdbinit文件中设置参数而不是在Arguments选项卡中设置(在Debug Configurations中),只需输入如下行:

设置args arg1 arg2

然后,它工作正常!

© www.soinside.com 2019 - 2024. All rights reserved.