c - GDB 输入文件等

标签 c file gdb

我正在尝试调试一个程序,但是我需要一个 txt 文件作为输入。我不确定我如何 gte 文本文件来玩这个程序。我编译为
gcc -g 文件名.c 文件名1.c
一个并且还在调试器中做一些事情。程序不断退出,因为文件为 NULL。如何获取要输入到程序中的txt文件?

最佳答案

编辑:

或者,您是否尝试在程序中使用 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]

关于c - GDB 输入文件等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10290898/

相关文章:

java - 如何从我的电脑打开和查看文件夹?

c++ - 相当于 Chrome `debugger`,适用于Cygwin的 `gdb`吗?

创建一个包含某一级别节点值的数组

c - 在作为值传递的结构地址上设置 NULL

android - 将文件 Assets 打包成apk?

c++ - 在 C++ 中使用 opendir 打开现有目录时出错

gdb 在 macOS 上不读取核心转储

c - 如何在 gdb 中调试带有信号处理程序的 C 程序?

c - char*** 的动态内存分配

python - 无法将 cython 生成的 C 语言代码转换为可执行文件