3

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

4

1 回答 1

4

编辑:

或者,您是否尝试在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]
于 2012-04-24T03:52:08.183 回答