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?

8
  • 1
    this should help stackoverflow.com/questions/4521015/… Commented Apr 24, 2012 at 2:27
  • Okay, I tried r < textfile.txt and it says, "Starting program... [New Thread...] <br> [New Thread...] <br> [Inferior 1 (process 3516) exited with code 01]" Is this a problem with input or a problem with my program? Commented Apr 24, 2012 at 2:32
  • does the program run when you run it not under gdb? Commented Apr 24, 2012 at 2:34
  • Yes, only half of it works, but it runs. Commented Apr 24, 2012 at 2:36
  • 1
    "[Inferior 1 (process 3516) exited with code 01]" is normal exit if the code return 1 or some other function return 1. Say: (gdb)b main (gdb) r < textfile.txt Commented Apr 24, 2012 at 3:22

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.