【问题标题】:How to give input to a program while debugging using mingw gdb如何在使用 mingw gdb 进行调试时为程序提供输入
【发布时间】:2013-07-25 00:08:49
【问题描述】:

我正在尝试使用 Mingw gdb 调试具有 scanf 语句的 C 程序。 我按照this post 编译程序并使用以下命令启动调试器

gcc -g -o sample sample.c 
gdb sample.exe            

并使用break main 在 main 处创建断点。由于我的程序需要一个整数和字符串作为 scanf 的输入,所以我关注了this post 并创建了一个名为 input 的文件,其中包含内容

1
"InputString"

并使用调试器运行程序

run < input

gdb 似乎没有启动程序并给出以下消息

Starting program: F:\spoj\sample.exe < input
Don't know how to run.  Try "help target".

请帮助我了解我哪里出错了。

【问题讨论】:

    标签: gdb mingw


    【解决方案1】:

    我使用以下格式为在 gdb 下运行的程序提供命令行参数:

    <shell> gdb a.out
    gdb> set args "what ever you would provide on the command line"
    gdb> run
    

    【讨论】:

    • 我必须在运行时使用 scanf 获取输入,所以我正在寻找一个 gdb 选项,它会提示输入输入。据我了解,您的程序采用命令行参数。
    • 如果您知道要通过 scanf 输入的输入,那么您可以通过 set args 提供它们吗?如果您只在运行时知道这些数据,那么一种选择是从程序本身读取输入,即您可以在 sample.exe 中编写一个循环来从具有特定名称的文件中读取输入吗?您提供给 scanf() 的输入可以重定向到一个文件,以便 sample.exe 可以读取它。尽我所能。
    • 谢谢。我没有想到这个选项。我会试试的。暂时赞成答案。如果我能找到更好的选择,我会等待。
    猜你喜欢
    • 2020-09-16
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多