【问题标题】:GDB input files and suchGDB 输入文件等
【发布时间】:2012-04-24 02:11:48
【问题描述】:

我正在尝试调试程序,但是我需要一个 txt 文件作为输入。我不确定如何让文本文件与程序一起玩。我编译为
gcc -g 文件名.c 文件名1.c
一个 并且还在调试器中做了一些工作。程序一直退出,因为文件为 NULL。如何将txt文件输入到程序中?

【问题讨论】:

  • 好吧,我试过了 r [新线程...]
    [劣质 1 (进程 3516) exited with code 01]" 这是输入问题还是我的程序有问题?
  • 不是gdb下运行程序是否运行?
  • 是的,只有一半有效,但可以运行。
  • "[Inferior 1 (process 3516) exited with code 01]" 如果代码返回 1 或其他函数返回 1,则 正常 退出。说:(gdb)b main@ 987654324@

标签: c file gdb


【解决方案1】:

编辑:

或者,您是否尝试在程序中使用fopen 打开“textfile.txt”?


仅举个例子/清楚:

首先;这是一个糟糕的编译行,当你遇到问题时更糟,但你可能会再次为我们简化它。

使用类似的东西:

 $ gcc -Wall -Wextra -pedantic -ggdb -o myprog mycode.c
#include <stdio.h>

int main(void)
{
    int i;

    while((i = getchar()) != EOF)
        putchar(i);
    return 1; /* Normally you would use 0, 1 indicate some error. */
}

在终端:

$ gdb ./my_prog
(gdb) r < textfile.txt
Starting program: /home/xm/devel/ext/so/my_prog < textfile.txt
Text text text
Text text text
Text text text
Text text text

[Inferior 1 (process 17678) exited with code 01]
(gdb) q

线程(错误的代码但是...):

#include <pthread.h>
#include <stdio.h>

void *say_hello(void *threadid)
{
    printf("Helllu!\n");
    pthread_exit(NULL);
}

void *read_stdin(void *threadid)
{
    int i;

    while((i = getchar()) != EOF)
        putchar(i);
    pthread_exit(NULL);
}

int main(void)
{
    pthread_t threads[2];
    pthread_create(&threads[0], NULL, read_stdin, (void*)0);
    pthread_create(&threads[1], NULL, say_hello,  (void*)1);
    pthread_exit(NULL);
}

在终端:

$ gdb ./my_prog
(gdb) r < textfile.txt
Starting program: /home/xm/devel/ext/so/my_prog < textfile.txt
[Thread debugging using libthread_db enabled]
[New Thread 0xb7fd9b70 (LWP 17843)]
Text text text
Text text text
Text text text
Text text text

[New Thread 0xb77d8b70 (LWP 17844)]
Helllu!
[Thread 0xb77d8b70 (LWP 17844) exited]
[Thread 0xb7fd9b70 (LWP 17843) exited]
[Inferior 1 (process 17840) exited normally]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 2011-05-28
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    相关资源
    最近更新 更多