3

I'm trying to debug a program however I need a txt file for input. I am unsure as how I gte the text file to fun with the program. I compiled as
gcc -g filename.c filename1.c
a < text.txt
and also doing some while already in the debugger. The program keeps exiting because the file is NULL. How do I get the txt file to be inputted into the program?

Sams
  • 197
  • 1
  • 3
  • 14

1 Answers1

4

Edit:

Or, are you trying to open "textfile.txt" with fopen inside your program?


Only to give an example / be clear:

First of; that is a bad compile line, even worse when you have problems, but then again you might simplified it for us.

Use something like:

 $ 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. */
}

In terminal:

$ 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

Threads (bad code but ...):

#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);
}

In terminal:

$ 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]
Morpfh
  • 4,033
  • 18
  • 26